【问题标题】:Multiple Play/Pause button with audio tag带有音频标签的多个播放/暂停按钮
【发布时间】:2015-03-26 11:51:27
【问题描述】:

我在同一代码中遇到了超过 1 个播放/暂停按钮的问题,我有一个表格,其中有一个按钮,然后是右侧单元格上的歌曲名称。

按钮效果很好,这里是代码(从网上得到的):

<script language="javascript">

function play_pause() {

  var myAudio = document.getElementById("myAudio");

  if (myAudio.paused) {
    myAudio.play();
  } else {
    myAudio.pause();
  }
}

</script>

还有音频部分

 <html>
    <table>
      <tr>
         <td>
     <audio preload="none" id="myAudio" src="music.mp3" type="audio/mpeg"</audio>          
     <img src="play.png"  width=30 height=30 border="0" onclick="play_pause()";>
        </td>
      </tr>
      <tr>
         <td>
     <audio preload="none" id="myAudio" src="music2.mp3" type="audio/mpeg"</audio>          
     <img src="play.png"  width=30 height=30 border="0" onclick="play_pause()";>
       </td>
       </tr>    
     </table>
</html>

所以每当我在我的网络上播放任何按钮时,它都会开始播放第一首歌曲,如果我尝试播放另一首歌曲,就像我取消暂停第一首歌曲一样(实际上只播放第一首歌曲,即使我采购了另一首歌曲) 我很确定是因为我的函数中缺少代码,并且因为两个音频标签都引用了相同的 ID,但是,我究竟必须在我的脚本上实现什么才能使我的页面正常工作?我已经尝试了很多东西,但不能真正让它按我想要的方式工作。 非常感谢一些建议,在此先感谢。

Ps:现在对我来说不是必需的,但是如果使用代码我还可以获得音频的更改图标,我的意思是当“点击”时我会更改为暂停图标,谢谢!

【问题讨论】:

    标签: javascript html html5-audio


    【解决方案1】:

    您可以像这样在对象之间放置差异

        function play_pause(player) {
    
          var myAudio = document.getElementById(player);
    
          if (myAudio.paused) {
            myAudio.play();
          } else {
            myAudio.pause();
          }
        }
    

    还有

        <html>
    <table>
      <tr>
         <td>
     <audio preload="none" id="myAudio1" src="music.mp3" type="audio/mpeg"</audio>          
     <img src="play.png"  width=30 height=30 border="0" onclick="play_pause('myAudio1')";>
        </td>
      </tr>
      <tr>
         <td>
     <audio preload="none" id="myAudio2" src="music2.mp3" type="audio/mpeg"</audio>          
     <img src="play.png"  width=30 height=30 border="0" onclick="play_pause('myAudio2')";>
       </td>
       </tr>    
     </table>
    

    编辑: 超过 4 名玩家使用此循环

       var songs=['song1.mp3','song2.mp3','song3.mp3','song4.mp3'];
       var players="<table>";
       for(var i=0;i<songs.length;i++){
           players+='<tr>
         <td>
     <audio preload="none" id="myAudio_'+i+'" src="'+songs[i]+'" type="audio/mpeg"</audio>          
     <img src="play.png"  width=30 height=30 border="0" onclick="play_pause(\'myAudio_'+i+'\')";>
        </td>
      </tr>';
       }
      players+="</table>"
     document.getElementById('myTable').innerHTML=players;
    

    和你的 html

     <body> <div id='myTable'></div>    </body>
    

    【讨论】:

    • 啊感谢您的快速回复!它现在可以工作了,但我注意到如果我有不止 2 个音频(这是我的情况),我认为我不能放尽可能多的 1、2、3、4 ......的歌曲是,我的意思是我必须声明一个常量,对吗?所以会为尽可能多的儿子工作或稍后添加任何建议都非常感谢!感谢回复
    • 好的,您可以按照我在上一个答案中的建议添加一个 for 循环。如果对您有帮助,请随时接受我的回答。
    • 对不起,但它不工作,或者我没有把它做对,无论如何我不得不说我不能添加这样的行,因为有更多的单元格可能会通过添加像这样的行,我仍然接受你的回答,因为非常有帮助
    • 嘿@Bellash 我希望你能再次提供帮助,我真的需要它,而且你似乎对你的工作非常了解,如果你看看我的另一篇帖子我真的很感激link 谢谢提前
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多