I can preload images easily thanks to the onload
function. But it doesn't work with audio. Browsers like Chrome, Safari, Firefox, etc. don't support the onload
functions in the audio tag.
How do I preload a sound in Javascript without using JS libraries and without using or creating HTML tags?
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);