2013-05-03 74 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

相關問題