Dotan
Dotan

Reputation: 7622

in html5, can I force the browser to download the audio files I'm using?

I'm programming html5 games, and using audio files like this

var sound = new Audio('sound.mp3');

when the command sound.play() appears at some point in the game the browser downloads and plays sound.mp3. this causes a delay in playing the sound on it's first appearance.

is there a way to force the browser to download all the audio files in advance to prevent this?

Upvotes: 2

Views: 738

Answers (1)

dsuckau
dsuckau

Reputation: 592

var sound = new Audio();

sound.preload = 'auto';

sound.addEventListener('canplaythrough', function () {
    sound.play(); // or other callback actions after preloading
});

document.body.appendChild(sound);

sound.src = 'sound.mp3';
sound.load();

Upvotes: 2

Related Questions