简体   繁体   English

是否可以在播放之前预加载整个HTML5视频源?

[英]Is it possible to preload an entire HTML5 video source before playing?

Solution

I created a working example of the accepted answer which uses an XHR and reports loading progress through a bar. 我创建了一个使用XHR并通过条形图报告加载进度的已接受答案的工作示例。

It's available here. 它可以在这里找到。

https://github.com/synthecypher/full-preload https://github.com/synthecypher/full-preload

Question

I've noticed when I create a <video> element with sources and call load() it will load to about 36% and then stops unless you play() it, at which time it'll continue to load the rest of the video as it plays. 我注意到当我创建一个带有源的<video>元素并调用load() ,它将加载到大约36%,然后停止,除非你play()它,此时它将继续加载剩下的视频因为它播放。

However, I want to ensure the entire video loaded before hand as it's an element in a timed exercise and if the internet connection drops during the exercise I will have to detect such an event and restart the exercise. 但是,我想确保整个视频在手动加载,因为它是定时练习中的元素,如果在练习期间互联网连接中断,我将不得不检测到这样的事件并重新开始练习。

I assume this is a built in feature of the HTML5 media elements but is it possible to override this native functionality? 我假设这是HTML5媒体元素的内置功能,但它是否可以覆盖此本机功能?

I've attempted to load the entire video source as a arraybuffer using an XMLHttpRequest which is then converted to a blob and set as the src of a <source> element in my <video> element. 我尝试使用XMLHttpRequest将整个视频源加载为arraybuffer ,然后将其转换为blob并设置为<video>元素中<source>元素的src

This does work however, it isn't ideal as I can't report the progress of download to the user through the use of progress bar as an XHR is a syncronous operation and will cause my JavaScript to hang until a response is received. 这确实有效,但它并不理想,因为我无法通过使用进度条向用户报告下载进度,因为XHR是一个同步操作,并且会导致我的JavaScript挂起,直到收到响应。 I know XHR2 now has this funtionality but I have to support IE8+ so that's not an option. 我知道XHR2现在有这种功能,但我必须支持IE8 +所以这不是一个选择。

Is there an easier more elegant solution to my problem, which will report progress? 是否有一个更简单,更优雅的解决方案来解决我的问题?

Requirements 要求

  • Need to preload the entire <video> element's <source> before playing. 在播放之前需要预加载整个<video>元素的<source>
  • Need to report progress of the 需要报告进度

If you have the source, you can pre-download the data to a blob in JavaScript and play when ready. 如果您有源代码,则可以预先将数据预先下载到JavaScript中的blob并在准备好时播放。

The following should work if the video is on your server. 如果视频在您的服务器上,则以下内容应该有效。 If not, you'll run into CORS issues. 如果没有,您将遇到CORS问题。

var video = document.getElementById("Your video element id")

var url = "Some video url"

var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = "arraybuffer";

xhr.onload = function(oEvent) {

    var blob = new Blob([oEvent.target.response], {type: "video/yourvideosmimmetype"});

    video.src = URL.createObjectURL(blob);

    //video.play()  if you want it to play on load
};

xhr.onprogress = function(oEvent) {

    if (oEvent.lengthComputable) {
        var percentComplete = oEvent.loaded/oEvent.total;
        // do something with this
    }
}

xhr.send();

Here is a sample to preload full video using XHR . 以下是使用XHR预加载完整视频的示例。 but you need to handle CORS by yourself. 但你需要自己处理CORS。

var xhrReq = new XMLHttpRequest();
xhrReq.open('GET', 'yourVideoSrc', true);
xhrReq.responseType = 'blob';

xhrReq.onload = function() {
    if (this.status === 200) {
        var vid = URL.createObjectURL(this.response);
        video.src = vid;
    }
}
xhrReq.onerror = function() {
    console.log('err' ,arguments);
}
xhrReq.onprogress = function(e){
    if(e.lengthComputable) {
        var percentComplete = ((e.loaded/e.total)*100|0) + '%';
        console.log('progress: ', percentComplete);
    }
}
xhrReq.send();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM