如何使用js判斷視頻是否可以成功訪問


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

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();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM