如何检查多个HTML5视频的音频

时间:2017-08-01 08:53:02

标签: javascript html5 audio video

我试图检查多个视频是否有音频,如下所示: 的 https://jsfiddle.net/hrd4a0c3/

但这仅检查一个视频,我希望这可以检查多个视频。

document.getElementById("myVideo").addEventListener("loadeddata", function() {
  if (typeof this.webkitAudioDecodedByteCount || this.mozHasAudio || this.audioTracks !== "undefined") {
    if (this.webkitAudioDecodedByteCount > 0 || this.mozHasAudio || this.audioTracks && this.audioTracks.length)
      this.after("has audio");
    else
      this.after("no audio");
  }
});

1 个答案:

答案 0 :(得分:0)

我会使用class代替id,因为ids应该引用一个元素。

<video class='myVideo' autoplay loop muted>
  <source src="https://vtt.tumblr.com/tumblr_ok41s7Iebh1t4fjz0_480.mp4" type="video/mp4">
</video>

<video class='myVideo' autoplay loop muted>
  <source src="https://vtt.tumblr.com/tumblr_ok44bpZZka1t4fjz0_480.mp4" type="video/mp4">
</video>

然后你可以这样使用getElementsByClassName

var videos = document.getElementsByClassName("myVideo");

for (i = 0; i < videos.length; i++) {
  videos[i].addEventListener("loadeddata", function() {
    if (typeof this.webkitAudioDecodedByteCount || this.mozHasAudio || this.audioTracks !== "undefined") {
      if (this.webkitAudioDecodedByteCount > 0 || this.mozHasAudio || this.audioTracks && this.audioTracks.length)
        this.after("has audio");
      else
        this.after("no audio");
    }
  });
}

更新了Fiddle