How can I force an HTML5 audio element to buffer an entire song?

George picture George · Jan 21, 2011 · Viewed 15.2k times · Source

I'm developing a local server that will stream a user's audio files so they can access them via web browsers using the HTML5 audio object. Since these files are on the user's computer, I expect the files to be buffered completely when they are loaded, but for certain large files, the songs get buffered part of the way, then stop, and resume buffering some time later.

My question is: how can I force the audio object to buffer the entire song at once? Can I do this from javascript, do I have to set an attribute on the audio object, or is there anything else I can do?

Answer

Ahi Tuna picture Ahi Tuna · Feb 10, 2012

The solution I found was this:

function load() {
    a.play();
    setTimeout("a.pause()", 10);
}

Play the file and pause it 10ms later, then the browser will buffer the entire song.