播放新曲目时自动暂停 mediaelement.js 音频播放器

发布于 2024-10-30 09:58:35 字数 267 浏览 5 评论 0原文

使用 mediaelement.js 音频 - 有谁知道当同一页面上有 2 个或更多音轨时,如何或是否可以在播放另一首曲目时自动暂停 1 首曲目? (我想像播放/暂停切换属性)

有一个我在这里谈论的使用 Flash 播放器的示例: http: //stockmusicboutique.com/background 但想知道这个 html 5 播放器是否可以实现。

using the mediaelement.js audio - Does anyone know how or if it's possible to have 1 track pause automatically when another is played when there are 2 or more audio tracks on the same page? (I guess like a play/pause toggle attribute)

there's an example of what I'm talking about here using a flash player: http://stockmusicboutique.com/background but was wondering if it would be possible with this html 5 player.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别把无礼当个性 2024-11-06 09:58:35

你可以这样做:

<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>

You could do something like this:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文