最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to preload an entire HTML5 video source before playing? - Stack Overflow

programmeradmin2浏览0评论

Solution

I created a working example of the accepted answer which uses an XHR and reports loading progress through a bar.

It's available here.

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.

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?

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.

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. I know XHR2 now has this funtionality but I have to support IE8+ so that's not an option.

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.
  • Need to report progress of the

Solution

I created a working example of the accepted answer which uses an XHR and reports loading progress through a bar.

It's available here.

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.

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?

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.

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. I know XHR2 now has this funtionality but I have to support IE8+ so that's not an option.

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.
  • Need to report progress of the
Share Improve this question edited Jul 1, 2017 at 18:16 jshbrntt asked Mar 3, 2015 at 18:38 jshbrnttjshbrntt 5,3848 gold badges32 silver badges63 bronze badges 8
  • Have you tried the preload attribute? – Oriol Commented Mar 3, 2015 at 18:43
  • You do know that the video tag is not supported by IE8, right? – jlowcs Commented Mar 3, 2015 at 18:43
  • @jlowcs: I'm wrapping it in videos which creates a Flash fallback if required. – jshbrntt Commented Mar 3, 2015 at 19:01
  • 1 @Oriol: Yes but preload="auto" will only preload it partially and in some cases on iOS not at all. – jshbrntt Commented Mar 3, 2015 at 19:03
  • @jlowcs does anybody actually still care about ie8? Must users that actually care about what you're doing will be using a modern browser. I also think the same way for app, I'm not making my apps hard to maintain because there are some still using android 2.3. Too bad, upgrade your device. :) – Panama Jack Commented Mar 3, 2015 at 19:12
 |  Show 3 more comments

2 Answers 2

Reset to default 14

If you have the source, you can pre-download the data to a blob in JavaScript and play when ready.

The following should work if the video is on your server. If not, you'll run into CORS issues.

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 . but you need to handle CORS by yourself.

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();
发布评论

评论列表(0)

  1. 暂无评论