Johan Sandén
Founder of Labyrint, a Digital Solutions Agency. Always curious and passionate about learning new things and meeting new people. Lets talk!

Issue with HTML5 Video continuous downloading

Amazon CloudFront Amazon S3 Google Chrome JavaScript jQuery

Background
I have been working a lot with HTML5 <video> and delivery from Amazon S3 / CloudFront. This has been very pleasant until I recently discovered that, if you dynamically add a <video> / <source> with JavaScript would cause the movie to finish downloading even if you remove the DOM element again or even changing the “src” attribute on the object. It wont cancel/abort the download!

Reasons for adding the object dynamically was that I wanted to have control of the want the movie would start loading and the source URL would vary depending on the client connection. Also the movie is 100MB+ so I would very much like to avoid multiple downloading of the file at the same time.

Example
Appending a video element to a DOM object with id #video-container using with jQuery.
By doing this this we add an video element in the div and start playing the movie when we get the event “canPlayThrough”.

<div id="video-container"></div>
function playMovie() {
 $("#video-container").append('
<video id="game-video" preload src="movie.mp4" type="video/mp4"></video>
');
// addEventListener, play when loaded //
$("#game-video").get(0).addEventListener('canplaythrough',function(a) {
 a.play();
}
if(action == "restart") {
// remove the content of the video-container dom //
 $("#video-container").html('');
 $("#game-video").attr('src' : '');
  playMovie();
}

Result
The above code would NOT cause Google Chrome to abort the downloading of the movie, in fact if we call the playMovie() multiple times the file will actually start downloading multiple times. This gets even worse if the client has a slow connection.

Solution
So how do we solve this? Instead of adding the video/src element dynamically we make sure that a video element in the html document is already created, but with empty properties. This would cause the Google Chrome to abort the download if we change the src property.

<div id="video-container">
<video id="game-video" preload src="" type="video/mp4"></video>
</div>
function playMovie() {
 // setting src of the video
 $("#game-video").attr('src' : 'movie.mp4');

// addEventListener, play when loaded //
$("#game-video").get(0).addEventListener('canplaythrough',function(a) {
 a.play();
}

if(action == "restart") {
// reset the src of the video dom //
 $("#game-video").attr('src' : '');
  playMovie();
}

Thats it! Hopefully this can help you out.


  • sortelyn

    You are the man! I will test this and get back … I have been looking at this problem for some time now – thanks for posting your experience anyway.

    • https://www.labyrint.com sounden

      No problems! Hope this will help you out.

  • sortelyn

    Well my research shows the following:

    Your fix did work partly and fixed the first part of the problem namely to display the video element at all. But I have 2 additions to your post, that you can consider to include:

    1) I have to call video.load() when I have changed the video (you call .play() so that probably have the same effect). If I don’t do this the last video I played will continue to play even though the HTML is updated and refreshed in JQM.

    2) This is worse on Iphone! If video is working or not, depends on whether you load external javascript in the beginning of your HTML – yes it seems very odd but seems to be related to some race condition between external video and external javascript/css. But it did change the behaviour of my application and is documented here:

    http://blog.noinc.com/2010/05/13/html5-video-tag-iphone-ipad-ihaveheadache/

    The final thing i need to figure out is that .load() does not seem to updated the video element on Iphone. If I can fix this I hope to have a dynamic video element for Iphone and PC. Then there are all the other platforms…

  • http://danconnor.com Dan Connor

    I’m working through this issue as well. Have you determined this to be a reliable fix? I’m finding it causes IE9 to crash entirely and iPhone to behave unpredictably.

    Imagine if you were downloading a feature film… the bandwidth implications are huge.

© 2013 Labyrint Digital Agency – Blog