日常工作中會出現各個公司網絡不互通的情況,視頻如果采用不互通的地址也無法播放,下面方法提供了檢測視頻是否可以播放的解決方案:
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();