简体   繁体   English

在JavaScript中使用HTML5音频标签

[英]Use of HTML5 audio tags with JavaScript

I want to set up audio links for a grid of 16 audio files using HTML, CSS and JavaScript. 我想使用HTML,CSS和JavaScript为16个音频文件的网格设置音频链接。 The idea is for a link to a separate audio file in each box in the grid. 这个想法是为了链接到网格中每个框中的单独的音频文件。

I thought using a table might work for this and have created one with HTML/CSS. 我认为使用表格可能对此有效,并使用HTML / CSS创建了表格。 I used TD cells as follows: 我按如下方式使用TD单元:

 <td id="A" class="squareA" type="button";><audio src="music/work_it.wav">Work It</td> 

I then tried to set up event listeners/getElementsByTagName using JS as follows but have got into a right old muddle. 然后,我尝试使用JS如下设置事件侦听器/ getElementsByTagName,但陷入了一个老问题。 If anyone could give me any pointers I'd be very grateful! 如果有人可以给我任何指示,我将不胜感激!

document.addEventListener("DOMContentLoaded", function(event)){
var audio = document.getElementsByTagName('audio')[0];

  workit.addEventListener('click' function());

You may need to target the audio tag inside each td , but as yet have no unique identifier to help with that task. 您可能需要将每个td内的audio标签作为目标,但目前尚没有唯一的标识符来帮助完成该任务。 A small addition to your code could solve this... 在您的代码中添加少量内容可以解决此问题...

<td id="A" class="squareA" type="button";>
   <audio id="audio-A">
     <source src="music/work_it.wav" type="audio/wav">
   </audio>
   Work It
</td>

Now you can find each tag by a specific id . 现在,您可以按特定的id查找每个标签。

Nonetheless, if you simply allow controls in the audio tag users will be able to click the default browser player and play the file without any further code. 但是,如果仅在audio标签中允许controls ,则用户将可以单击默认浏览器播放器并播放文件,而无需任何其他代码。

<audio controls>
   <source ... etc
</audio>

However if controls are not present (or set to controls="false" ) the default browser player will not appear. 但是,如果不存在controls (或将controls="false"设置为controls="false" ),则默认浏览器播放器将不会出现。 I presume this is what you're after because you're looking to play the audio-file with a click-event. 我想这就是您要追求的,因为您希望通过单击事件来播放音频文件。

Audio-tag id s aren't necessarily helpful at this point as without controls nothing is displayed in the browser to click on, in which case you'll need to target all their containing td tags in the table. 音频标签id不一定对您有帮助,因为如果没有controls ,浏览器中不会显示任何内容以供单击,在这种情况下,您需要将表中所有其包含的td标签作为目标。

If you haven't already done so add an id attribute to the audio table (eg: 'audio-table'), then you can .addEventListener() to each td-box with a function called on page load; 如果尚未添加id属性到音频表(例如“ audio-table”),则可以使用在页面加载时调用的函数将.addEventListener()放入每个td-box; something like... 就像是...

function addEvents2Table() {
  /* presuming the table has an
     id of 'audio-table' here */
  var audioTable = document.getElementById('audio-table');

  /* td tag collection */
  var tdTags = audioTable.getElementsByTagName('td');
  var len = tdTags.length;

  /* add a 'playAudio' function call
     to each td when clicked */
  while(len--) {
    tdTags[len].addEventListener(
      'click', playAudio, false
    );
  }
}

/* call 'addEvents2Table()' on page load */
window.onload = function() {
  addEvents2Table();
};

Now when a td in the table is clicked the playAudio() function will fire. 现在,当单击表中的tdplayAudio()函数将启动。 All that's needed now is that function. 现在所需的就是该功能。 This is where paying attention to the html markup can really help. 在这里,注意html标记确实可以提供帮助。

In the first code-example above I added id="audio-A" to the audio-tag inside a td-tag with id="A" . 在上面的第一个代码示例中,我在id="audio-A"的td标签内的音频标签中添加了id="audio-A" id="A" That 'A' (or 'B' or 'C' etc) can come in really handy now. “ A”(或“ B”或“ C”等)现在可以派上用场了。

function playAudio(event) {
  /* get the 'id' of the clicked element */
  var tagID = event.target.id;

  /* add it to 'audio-' */
  var audioID = 'audio-' + tagID;

  /* target the corresponding audio tag */
  var audioTAG = document.getElementById(audioID);

  /* play the file */
  audioTAG.play();
}

There are other things to consider here. 这里还有其他事情要考虑。 Do you want the browser to play audio files simultaneously? 您是否希望浏览器同时播放音频文件? Do you want users to be able to stop an audio clip? 您是否希望用户能够停止音频剪辑? As we have it now you could click all the boxes in quick succession and the browser would try and play all the audio files over each other. 现在我们有了它,您可以快速连续单击所有框,浏览器将尝试相互播放所有音频文件。 Also if you click a box while that audio file is playing you'll throw up some issues. 另外,如果在播放音频文件时单击一个框,则会引发一些问题。

Still, that should be enough to get you started. 尽管如此,这应该足以让您入门。 Please comment if you need further clarification. 如果您需要进一步说明,请发表评论。

In the meantime, here's MDN's audio-tag page for reference. 同时,这是MDN的音频标签页,仅供参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM