How to preload a sound in Javascript? How to preload a sound in Javascript? javascript javascript

How to preload a sound in Javascript?


Your problem is that Audio objects don't support the 'load' event.

Instead, there's an event called 'canplaythrough' that doesn't mean it's fully loaded, but enough of it is loaded that at the current download rate, it will finish by the time the track has had enough time to play through.

So instead of

audio.onload = isAppLoaded;

try

audio.oncanplaythrough = isAppLoaded;

Or better yet.. ;)

audio.addEventListener('canplaythrough', isAppLoaded, false);


I tried the accepted answer by tylermwashburn and it didn't work in Chrome. So I moved on and created this and it benefits from jQuery. It also sniffs for ogg and mp3 support. The default is ogg because some experts say a 192KBS ogg file is as good as a 320KBS MP3, so you save 40% on your required audio downloads. However mp3 is required for IE9:

// Audio preloader$(window).ready(function(){  var audio_preload = 0;  function launchApp(launch){    audio_preload++;    if ( audio_preload == 3 || launch == 1) {  // set 3 to # of your files      start();  // set this function to your start function    }  }  var support = {};  function audioSupport() {    var a = document.createElement('audio');    var ogg = !!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, ''));    if (ogg) return 'ogg';    var mp3 = !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));    if (mp3) return 'mp3';    else return 0;  }  support.audio = audioSupport();  function loadAudio(url, vol){    var audio = new Audio();    audio.src = url;    audio.preload = "auto";    audio.volume = vol;    $(audio).on("loadeddata", launchApp);  // jQuery checking    return audio;  }  if (support.audio === 'ogg') {    var snd1 = loadAudio("sounds/sound1.ogg", 1);  // ie) the 1 is 100% volume    var snd2 = loadAudio("sounds/sound2.ogg", 0.3);  // ie) the 0.3 is 30%    var snd3 = loadAudio("sounds/sound3.ogg", 0.05);        // add more sounds here  } else if (support.audio === 'mp3') {     var snd1 = loadAudio("sounds/sound1.mp3", 1);    var snd2 = loadAudio("sounds/sound2.mp3", 0.3);    var snd3 = loadAudio("sounds/sound3.mp3", 0.05);        // add more sounds here  } else {    launchApp(1);  // launch app without audio  }// this is your first function you want to start after audio is preloaded:  function start(){     if (support.audio) snd1.play();  // this is how you play sounds  }});

Furthermore:Here is an mp3 to ogg converter:http://audio.online-convert.com/convert-to-oggOr you can use VLC Media player to convert.Check your mp3 bitrate by right-clicking on the mp3 file (in Windows) and going to the file details. Try to reduce by 40% when selecting your new bitrate for your new 'ogg' file. The converter may throw an error, so keep increasing the size until is accepted. Of course test sounds for satisfactory quality. Also (and this applied to me) if you are using VLC Media player to test your audio tracks make sure you set the volume at 100% or below or otherwise you'll hear audio degradation and might mistakenly think it is the result of compression.


Depending on your target browsers, setting the prelaod attribute on the audio tag may be sufficient.