最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - stopping html5 audio - Stack Overflow

programmeradmin4浏览0评论

How would I write a function that stops ALL instances of playing html5 audio in the DOM?

html5 audio

<audio class="AudioPlayerV1" preload="none" width="292">
    <source src="test.mp3" type="audio/mpeg" />
</audio>

js

AudioPlayerV1('*').each(function() {
    this.setStop();
});

How would I write a function that stops ALL instances of playing html5 audio in the DOM?

html5 audio

<audio class="AudioPlayerV1" preload="none" width="292">
    <source src="test.mp3" type="audio/mpeg" />
</audio>

js

AudioPlayerV1('*').each(function() {
    this.setStop();
});
Share Improve this question asked Feb 14, 2012 at 20:23 BlainerBlainer 2,70210 gold badges38 silver badges40 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9
$.each($('audio'), function () {
    this.pause();
});

This just finds all the <audio> elements in the DOM and uses the standard API to pause the playback for each.

Here's some excellent reading about <audio> and <video> tags: https://developer.mozilla/en/Using_HTML5_audio_and_video

Update

The documentation for the AudioPlayerV1 script states this is how you pause an <audio> element:

$('#audio_id').AudioPlayerV1('pause');

Source: http://1.s3.envato./files/14653378/index.html#how-to-use

It looks like the AudioPlayerV1 plugin is supposed to be called on a selection of DOM element(s) and passed in an option.

Update

To run the plugin to pause multiple <audio> elements you should be able to do this:

$('audio').AudioPlayerV1('pause');

Which selects the <audio> elements in the DOM and passes them to the AudioPlayerV1 plugin. the plugin should be written to handle this but if it isn't then you have to call the plugin on each element. Inside a loop would be easiest:

$.each($('audio'), function () {
    $(this).AudioPlayerV1('pause');
});

first you have to set an id for your audio element

in your js :

var ply = document.getElementById('player');

var oldSrc = ply.src;// just to remember the old source

ply.src = "";// to stop the player you have to replace the source with nothing

This worked for me. .stop() threw an error.

$.each($('audio'), function () {
    this.pause();
});
发布评论

评论列表(0)

  1. 暂无评论