2013-05-03 72 views
0

我正在创建专门为ipad的页面。如何阻止一个音频播放器播放时,我点击另一个 - javascript

我在一页上有12个音频播放器。他们都很好,但如果你点击第二个玩家,第一个玩家将继续玩。我以前见过这个问题,但没有一个答案是特定于我的代码的。

<script> 

function EvalSound(soundobj) { 
var thissound=document.getElementById(soundobj); 
    if (thissound.paused) 
      thissound.play(); 
    else 
     thissound.pause(); 
     thissound.currentTime = 0; 
} 
</script> 

玩家自己:

<a href="card3_ipad.php?card=funny" onClick="EvalSound('song1'); return true;" 
    target="player"><IMG SRC="images/funnytab.gif" BORDER=0 WIDTH=128 HEIGHT=29> 
</A>  
<audio id="song1" style="display: none; width: 0px; height: 0px;" 
src="mp3examples/funnyauto.mp3" controls preload="auto" autobuffer> 

然后另一名球员是:

<a href="card3_ipad.php?card=country" onClick="EvalSound('song2'); return true;" 
    target="player"><IMG SRC="images/countrytab.gif" BORDER=0 WIDTH=128 HEIGHT=29> 
</A>  
<audio id="song2" style="display: none; width: 0px; height: 0px;" 
src="mp3examples/countryauto.mp3" controls preload="auto" autobuffer> 

任何帮助将大规模感激!

干杯

汤姆

回答

0

使用另一个变量持有当前活跃的球员

<script> 
var currentPlayer; 
function EvalSound(soundobj) { 

var thissound=document.getElementById(soundobj); 
if(currentPlayer && currentPlayer != thissound) { 
     currentPlayer.pause(); 
} 
if (thissound.paused) 
      thissound.play(); 
    else 
     thissound.pause(); 
     thissound.currentTime = 0; 
     currentPlayer = thissound; 
} 
</script> 
+0

谢谢你们非常迅速的反应!不幸的是,现在没有什么玩法:( – 2013-05-03 15:32:38

+0

@TomWilliams。现在修复它应该工作 – Anoop 2013-05-03 15:39:00

+0

再次感谢:)这次几乎可以使用!如果玩家第一次使用它不起作用。如果它已经启动一次然后代码工作。如果这首歌正在播放,那么它不会停止前一个播放器。 – 2013-05-03 15:44:59

相关问题