Wednesday, 19 February 2014

How to prefetch video/audio files for uninterrupted playback in HTML5 video/audio

Sometimes when you're playing a media file using an HTML5 <video> or <audio> element, or with WebAudio, you really want to be sure that the whole audio/video file is totally downloaded before you start playing it. For example, you may be writing a game, and you want to be sure all your sound effects are preloaded, so there's no delay between your animations and your sound effects while the network downloads the remainder of the file.

So how can you be sure a media resource is fully downloaded before beginning playing it? You could wait for the "canplaythrough" event to fire on all your media elements, but that event is not fired correctly by Chrome.

A more reliable solution is to prefetch the video/audio file using XHR/AJAX requests, and play the video/audio from a Blob URI.

Here's a simple snippet of a JS file that downloads a file using XHR. The function accepts callbacks to return results.

function prefetch_file(url,
                       error_callback) {
  var xhr = new XMLHttpRequest();"GET", url, true);
  xhr.responseType = "blob";

  xhr.addEventListener("load", function () {
    if (xhr.status === 200) {
      var URL = window.URL || window.webkitURL;
      var blob_url = URL.createObjectURL(xhr.response);
    } else {
  }, false);

  var prev_pc = 0;
  xhr.addEventListener("progress", function(event) {
    if (event.lengthComputable) {
      var pc = Math.round((event.loaded / * 100);
      if (pc != prev_pc) {
        prev_pc = pc;

When the file is successfully downloaded, the fetched_callback is called with an argument which is the blob URI. You can simply set this as the src of an audio or video element and can then play the fully-downloaded resource. You can also set the same blob URI as the src of multiple audio/video elements, and the downloaded data won't be re-downloaded or duplicated/copied in memory.

There's also a progress_callback that's called with a percentage complete parameter as the file is downloaded, and an error_callback that's called when the download fails.

For a working demo: prefetching a video file before playback using HTML5 demo


ev said...

Good stuff Chris.

I've noticed that Safari 6.1.3 doesn't function correctly. Any insight on why that is? It does however seem to display the progress as expected. The video player fails to load the file. No errors in the console. Maybe codec incompatibility, or blob issues?

Can you also explain the issues you mentioned concerning "canplaythrough" in chrome?


Chris Pearce said...

@ev: I don't have a Mac, so I can't offer insights into why it wouldn't work in Safari sorry.

Last time I checked, the "canplaythrough" event in Chrome fired as soon the media load is started, not when the remaining time to download is less than the duration of remaining media to play, as required by the specification.