单击链接时重置音频

时间:2015-08-09 20:19:53

标签: javascript html5 audio

当我播放音频时,暂停音频,点击外部链接,然后返回音频所在的页面,我希望音频重置而不是离开我离开的地方,但它离开了我的位置离开了。

在我的HTML5页面中,我在关闭正文标记之前添加了此链接:    SRC =" HTTPS://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">

HTML5:

// Create adapter, this happens in parent Activity or Fragment of RecyclerView
adapter = new Adapter<Person>(){
    @Override
    public PersonHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.layout_person_view, parent, false);

        PersonHolder holder = new PersonHolder(v);
        v.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                int itemPos = holder.getAdapterPosition();
                Person person = getItem(itemPos);

                // do something with person
                EventBus.getDefault().postSticky(new PersonClickedEvent(itemPos, person));
            }
        });

        return holder;
    }
};

JS:

<table id="multiplemp3">
    <tr>
       <th>
        <audio controls>
            <source src="http://geo-samples.beatport.com/lofi/5005876.LOFI.mp3" type="audio/mpeg">
Your browser does not support the audio element.
            </audio>
    </th>
</tr>
<tr>
    <th>
        <audio controls>
            <source src="http://geo-samples.beatport.com/lofi/5005933.LOFI.mp3" type="audio/mpeg">
Your browser does not support the audio element.
        </audio>
    </th>
</tr>
<tr>
</tr>
</table>
<br>


<p><a href="https://en.wikipedia.org/wiki/Main_Page">More . . .</a></p>
<p><a href="https://en.wikipedia.org/wiki/Main_Page">Return to Home Page</a></p>

http://jsfiddle.net/0eun4kcy/1/

1 个答案:

答案 0 :(得分:0)

您可以在暂停按钮上有一个事件监听器,然后自动将音频的currentTime设置为零。例如,如果您有一个音频元素,则可以:

var audio = document.getElementsByTagName("audio");

audio.onpause = function() {
     audio.currentTime = 0;
};

这将重置音频文件。下次用户单击暂停按钮时,它将从头开始播放。