Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can preload using <code>&lt;audio /&gt;</code> for newer browsers. Set <code>autoplay = false</code>. For older browsers that don't support <code>&lt;audio /&gt;</code>, you can use <code>&lt;bgsound /&gt;</code>. To preload a sound, set the volume to <code>-10000</code>.</p> <pre><code>function preloadSound(src) { var sound = document.createElement("audio"); if ("src" in sound) { sound.autoPlay = false; } else { sound = document.createElement("bgsound"); sound.volume = -10000; } sound.src = src; document.body.appendChild(sound); return sound; } </code></pre> <p>That will get the sound in your browser's cache. Then to play it, you can keep doing what you are doing with <code>&lt;embed /&gt;</code>. Or if you want to take advantage of HTML5 features, you can call <code>.play()</code> on the returned <code>&lt;audio /&gt;</code> element. You could even add a play method to the <code>&lt;bgsound /&gt;</code>:</p> <pre><code>function loadSound (src) { var sound = document.createElement("audio"); if ("src" in sound) { sound.autoPlay = false; } else { sound = document.createElement("bgsound"); sound.volume = -10000; sound.play = function () { this.src = src; this.volume = 0; } } sound.src = src; document.body.appendChild(sound); return sound; } </code></pre> <p>Then use it like this:</p> <pre><code>var sound = loadSound("/mySound.ogg"); // preload sound.play(); </code></pre> <p>The only caveat is FireFox doesn't support mp3. You'll have to convert your files to ogg.</p> <p>Working demo: <a href="http://jsfiddle.net/PMj89/1/" rel="noreferrer">http://jsfiddle.net/PMj89/1/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload