日常工作中会出现各个公司网络不互通的情况,视频如果采用不互通的地址也无法播放,下面方法提供了检测视频是否可以播放的解决方案:
1、跨域
var video = document.createElement('video');
video.onload = function() {
alert('success, it exsist');
// show video element
}
video.onerror = function() {
alert('error, couldn\'t load');
// don't show video element
}
video.src = 'http://www.example.com/video.mp4';
//不同浏览器情况不同,这里判断在该浏览器是否可以播放
video.oncanplaythrough = function() {
alert("This file can be played in the current browser");
};
2、不跨域
var http = new XMLHttpRequest();
http.open('HEAD', '/folder/video.mp4');
http.onreadystatechange = function() {
if (this.readyState == this.DONE) {
if (this.status != 404) {
// resource exists
}
}
};
http.send();