Reputation: 7622
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
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