HTML onclick 中的多个音频文件的问题

Posted

技术标签:

【中文标题】HTML onclick 中的多个音频文件的问题【英文标题】:Issues with multiple audio files in HTML onclick 【发布时间】:2018-07-02 03:31:46 【问题描述】:

我正在创建一个具有 10 个键的在线乐器,每个键都有一个录制的声音,可以在点击时播放。目前它只播放第一个声音,我无法弄清楚我需要在代码中做什么才能播放其他音频文件。我查看了多个链接以尝试解决此问题,但没有任何效果!

<h1> Click to Play 

<script>  function play()
   var audio = document.getElementById("audio");
   audio.play();
             
 </script> <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473 /Low_2.jpeg' value="PLAY"  onclick="play()"><audio id="audio" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/low%202.mp3" ></audio>

 <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/Low_3.jpeg' value="PLAY"  onclick="play()"><audio id="audio" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/low%203.mp3" ></audio>


 <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/Low_5.jpeg' value="PLAY"  onclick="play()"><audio id="audio" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/low%205.mp3" ></audio>

<img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/Low_6.jpeg' value="PLAY"  onclick="play()"><audio id="audio" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/low%206.mp3" ></audio>

<img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/Low_1.jpeg' value="PLAY"  onclick="play()"><audio id="audio" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/low%201.mp3" ></audio>

<img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/High_2.jpeg' value="PLAY"  onclick="play()"><audio id="audio" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/high%202.mp3" ></audio>

<img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/High_3.jpeg' value="PLAY"  onclick="play()"><audio id="audio" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/High%203.mp3" ></audio>

<img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/High_5.jpeg' value="PLAY"  onclick="play()"><audio id="audio" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/High%205.mp3" ></audio>

<img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/High_6.jpeg' value="PLAY"  onclick="play()"><audio id="audio" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/high%206.mp3" ></audio>

<img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/High_1.jpeg' alt=''>



</body>
<html>

【问题讨论】:

尽量减少代码中的多余空格。 【参考方案1】:

您应该将 Id 发送到 javascript 函数或使用不同的 id

第一个解决方案

<h1> Click to Play </h1>

<script>  function play(id)
   var audio = document.getElementById('audio'+id);
   audio.play();
             
 </script> <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473 /Low_2.jpeg' value="PLAY"  onclick="play('1')"><audio id="audio1" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/low%202.mp3" ></audio>

 <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/Low_3.jpeg' value="PLAY"  onclick="play('2')"><audio id="audio2" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/low%203.mp3" ></audio>    

</body>
<html>

第二种解决方案:

你可以像这样使用 jquery:

<h1> Click to Play </h1>

<script>
$(document).ready(function()
    $('img').click(function() 
        $(this).first('audio').play();
    );

);

</script>
 <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473 /Low_2.jpeg' value="PLAY" ><audio id="audio1" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/low%202.mp3" ></audio>

     <img src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/Low_3.jpeg' value="PLAY" ><audio id="audio2" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2124473/low%203.mp3" ></audio>    

    </body>
    <html>

【讨论】:

以上是关于HTML onclick 中的多个音频文件的问题的主要内容,如果未能解决你的问题,请参考以下文章

按顺序播放多个音频文件(Javascript)

多个动态音频播放器应用于 HTML/JavaScript 中的字典

创建新的随机音频文件时,播放随机音频 onClick 不会重置 - Android

尝试使用 onclick 在 html 视频上切换声音

使用 jquery 将 onclick 事件添加到音频时间线

如何将多个 onClick 事件链接到 HTML 中的一个元素?我可以让他们改变href吗?