XMLHttpRequest to download large HTML5 video in chunks?

前端 未结 1 1864
不知归路
不知归路 2021-01-14 04:52

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         


        
相关标签:
1条回答
  • 2021-01-14 05:36

    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.

    0 讨论(0)
提交回复
热议问题