我正在播放一个小音频剪辑点击每个链接在我的导航

HTML代码:

<audio tabindex="0" id="beep-one" controls preload="auto" >
    <source src="audio/Output 1-2.mp3">
    <source src="audio/Output 1-2.ogg">
</audio>

JS代码:

$('#links a').click(function(e) {
    e.preventDefault();
    var beepOne = $("#beep-one")[0];
    beepOne.play();
});

到目前为止一切正常。

问题是当一个声音剪辑已经运行,我点击任何链接没有发生。

我试图停止点击链接时已经播放的声音,但在HTML5的音频API中没有直接事件

我试着遵循代码,但它不工作

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

有什么建议吗?


当前回答

如果你的网站上有几个音频播放器,你想暂停它们:

$('audio').each( function() {
    $(this)[0].pause();
});

其他回答

shamangeorge写道:

手动设置currentTime可以触发音频元素上的canplaythrough事件。

This is indeed what will happen, and pausing will also trigger the pause event, both of which make this technique unsuitable for use as a "stop" method. Moreover, setting the src as suggested by zaki will make the player try to load the current page's URL as a media file (and fail) if autoplay is enabled - setting src to null is not allowed; it will always be treated as a URL. Short of destroying the player object there seems to be no good way of providing a "stop" method, so I would suggest just dropping the dedicated stop button and providing pause and skip back buttons instead - a stop button wouldn't really add any functionality.

首先,你必须为音频元素设置一个id

在js中:

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

var oldSrc = cp .src;//只是为了记住旧的源代码

厚度。SRC = "";//要停止播放,你必须将源代码替换为空

我也有同样的问题。如果它是一台收音机,停止应该停止流和播放去直播。我看到的所有解决方案都有一个缺点:

的球员。currentTime = 0继续下载流。 的球员。SRC = "引发错误事件。

我的解决方案:

var player = document.getElementById('radio');
player.pause();
player.src = player.src;

还有HTML

<audio src="http://radio-stream" id="radio" class="hidden" preload="none"></audio>

这有什么问题吗?

audio.load()

如规范和MDN中所述:

此元素之前播放的任何媒体资源的回放停止。

调用load()将中止所有涉及该媒体元素的正在进行的操作

在IE 11中,我使用了组合变体:

player.currentTime = 0; 
player.pause(); 
player.currentTime = 0;

只有2次重复可以防止IE在pause()后继续加载媒体流,并因此淹没磁盘。