使用mediaelement.js音频 - 有人知道当同一页面上有2个或更多音轨时播放另一个音轨时,是否可以自动暂停1个音轨? (我想像播放/暂停切换属性一样)
这里有一个我正在使用flash播放器讨论的例子:http://stockmusicboutique.com/background但是想知道这个html 5播放器是否可行。
答案 0 :(得分:3)
你可以这样做:
<audio src="track1.mp3" />
<audio src="track2.mp3" />
<script type="text/javascript">
// make an array for the mediaelement players
mediaElementPlayers = new Array();
// run mediaelement.js
$(document).ready(function(){
$('audio').mediaelementplayer({
success:function(mediaElement, domObject){
// add this mediaelement to the mediaElementPlayers array
mediaElementPlayers.push(mediaElement);
// bind the play event to the pauseAllPlayers function
mediaElement.addEventListener('play', function(e){
pauseAllPlayers(e.target); }
)
}
})
})
// iterate through the mediaElementPlayers array, pause all players except the one that triggered the event.
function pauseAllPlayers(currentPlayer){
for(i=0; i<mediaElementPlayers.length; i++){
if(mediaElementPlayers[i] != currentPlayer){
mediaElementPlayers[i].pause();
}
}
}
</script>