视频具有音轨并且其音量不为零或静音,但由于其音轨完全安静,因此该视频无法听到。我想知道如何检查视频是否可听,因此我可以切换功能。
我找到了如何从这里检查视频是否有音轨。除了IE10 +之外,@ upuoth的方法适用于大多数浏览器。 @ brefd.it适用于IE10 +,但我不知道如何使用他的代码(请向我解释)
@ upuoth的代码
document.getElementById("video").addEventListener("loadeddata", function() {
if (typeof this.webkitAudioDecodedByteCount !== "undefined") {
// non-zero if video has audio track
if (this.webkitAudioDecodedByteCount > 0)
console.log("video has audio");
else
console.log("video doesn't have audio");
}
else if (typeof this.mozHasAudio !== "undefined") {
// true if video has audio track
if (this.mozHasAudio)
console.log("video has audio");
else
console.log("video doesn't have audio");
}
else
console.log("can't tell if video has audio");
});
@ brefd的代码
function hasAudio (video) {
return video.mozHasAudio ||
Boolean(video.webkitAudioDecodedByteCount) ||
Boolean(video.audioTracks && video.audioTracks.length);
}
var video = document.querySelector('video');
if(hasAudio(video)) {
console.log("video has audio");
} else{
console.log("video doesn't have audio");
}
我准备了三个视频片段,一个带有音轨,一个没有音轨,一个带有音轨,但听不见。我上面找到的代码仅适用于fisrt两个剪辑,但适用于第三个剪辑。使用code from JSfiddle link播放第三个剪辑时,请让警报“听不见”。
答案 0 :(得分:0)
以下是您可以在JSFiddle中替换的合并代码:
document.getElementById("myVideo").addEventListener("loadeddata", function() {
if (typeof this.webkitAudioDecodedByteCount !== "undefined") {
if (this.webkitAudioDecodedByteCount > 0)
alert("This video has a track");
else
alert("no track");
} else if (typeof this.mozHasAudio !== "undefined") {
if (this.mozHasAudio)
alert("This video has a track");
else
alert("no track");
} else if (typeof this.audioTracks !== "undefined") {
if (this.audioTracks && this.audioTracks.length)
alert("audible");
else
alert("not audible");
} else
alert("Not sure");
});
不幸的是,我怀疑是否有办法检测"听不到"音轨。