Sound effects in JavaScript / HTML5

HTML5 Audio objects

You don’t need to bother with <audio> elements. HTML 5 lets you access Audio objects directly:

var snd = new Audio("file.wav"); // buffers automatically when created
snd.play();

There’s no support for mixing in current version of the spec.

To play same sound multiple times, create multiple instances of the Audio object. You could also set snd.currentTime=0 on the object after it finishes playing.


Since the JS constructor doesn’t support fallback <source> elements, you should use

(new Audio()).canPlayType("audio/ogg; codecs=vorbis")

to test whether the browser supports Ogg Vorbis.


If you’re writing a game or a music app (more than just a player), you’ll want to use more advanced Web Audio API, which is now supported by most browsers.

Leave a Comment