I am trying to load a large video into a tag using XMLHttpRequest. I\'ve successfully gotten this to work with small video files using the following code:
w
It's unfortunate that the browser needs to do so much copying in this scenario - the code does seem like it should work, since you're creating a blob URL and not a super long dataURI string. But sure enough, this method is very slow. However, there is another solution that should give you what you want without messing about with XHR.
Create a video element and set src
to your mp4 (or webm for firefox) file. Add a listener for the 'progress' event, which should fire regularly as the browser downloads the file. In that listener, you can check the buffered
property to see how much of the video has downloaded and make your own decision as to whether you're ready to start playing.
Now, here it gets a little bit ugly. Even with preload, the browser still will only buffer a little bit of the video, probably about the same amount that it needs to fire canplaythrough
. You'll only get one or two progress events, and then nothing. So, when you create the video element, set it to autoplay
, and then hide it and mute it. That should force the browser to download the rest of the video and trigger more progress
events. When it's fully buffered, or enough to satisfy you, set currentTime
back to zero, unmute and unhide.