我正在尝试使用这两个简单的功能来检查URL是否包含音频或视频:
//For video
var video = document.createElement("video");
video.setAttribute("src", url);
video.addEventListener("canplay", function() {
console.log("video true");
});
video.addEventListener("error", function() {
console.log("video false");
});
//For audio
var audio = new Audio();
audio.setAttribute("src", url);
audio.addEventListener("canplay", function() {
console.log("audio true");
});
audio.addEventListener("error", function() {
console.log("audio false");
});
问题是它们都对有效的音频和视频URL都返回true,如何解决?
因此,经过深入研究,我发现(HTML)音频实际上是没有尺寸的视频,因此您可以简单地使用它:
var video = document.createElement("video");
video.setAttribute("src", url);
video.addEventListener("canplay", function() {
console.log("video true");
console.log(video.videoHeight);
});
video.addEventListener("error", function() {
console.log("video false");
});
//audio => video true
// 0
//video => video true
// 1080