簡體   English   中英

用戶播放下一個音頻時如何停止播放音頻?

[英]How to stop audio to play when user plays next audio?

我的網站上有幾個音樂播放器,它們都來自相同的插件

<audio id="player2" src="//filename" type="audio/mp3" controls="controls"></audio> 

當用戶從另一個播放器播放音頻時,我希望之前的音頻播放器停止。 我是初學者所以希望有一個簡單的解釋。

您需要收聽play事件並在其他播放器上運行pause

var audios = document.getElementsByTagName('audio'); // We get a list of all audio elements
audios.forEach(function(player) { 
  player.addEventListener('play', function() { // We listen for the `play` event on each player
    audios.forEach(function(player2) {
      if (player !== player2) { // we find other players which don't match the one that started
        player2.pause(); // we pause those other players
      }
    });
  });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM