如何在整个DOM上停止(并将当前时间重置为0)所有音频实例,甚至是隐藏的?我无法使用此代码停止当前播放的音频。
$('.link').on('click', function(e){
$('.pagecontainer>div').fadeOut();
$(this.getAttribute("href")).fadeIn();
stopAudio();
});
function stopAudio() { //fade out and stop any current audio
$('audio').animate({volume: 0}, 1000);
setTimeout(function(){
$.each($('audio'), function () {
this.currentTime=0;
this.pause();
});
},1000)
}
$('.sound').on('play', function () { //since volume was faded out, reset volume when click play button
$('audio').animate({volume: 1}, 100);
});
....
....
<a href="#page1" class="link">Audio 1</a>
<a href="#page2" class="link">Audio 2</a>
<a href="#page3" class="link">Audio 3</a>
<div class="pagecontainer">
<div id="page1"> //all three audio elements are in exact same spot
//clicking page link fades in current audio and fades in new one
<audio controls loop class="sound">
<source src="../../site/music/music1.mp3"/>
<source src="../../site/music/music1.ogg"/>
</audio>
</div>
<div id="page2">
<audio controls loop class="sound">
<source src="../../site/music/music2.mp3"/>
<source src="../../site/music/music2.ogg"/>
</audio>
</div>
<div id="page3">
<audio controls loop class="sound">
<source src="../../site/music/music3.mp3"/>
<source src="../../site/music/music3.ogg"/>
</audio>
</div>
</div>
当我点击指向另一个页面的链接时,我需要停止并重置所有三个音频元素。目前,由于它没有停止,当我点击淡入的音频元素的播放按钮时,我听到两个音频同时播放。虽然有趣的是音频确实消失了。
请注意,所有三个音频元素都在完全相同的位置。