2017-08-01 65 views
0

我想檢查是否多個視頻音頻或沒有,像這樣: https://jsfiddle.net/hrd4a0c3/如何檢查多個HTML5視頻音頻

但這只是檢查一個視頻,我想這個檢查多個視頻。

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"); 
    } 
}); 

回答

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

+0

好感謝的人,它的工作 – metalbug

+0

沒問題:)我很高興它幫助。 –

+0

but'this.after(「

no audio

」);'不工作 – metalbug