Introduction to the HTML5 audio tag javascript manipulation

by Cedric Dugas on July 12, 2009

Some of the new exciting features of HTML5 are the audio and video tag. This could potentially, in the long term, replace flash on the video level. I wanted to use the audio tag in one of my experiemental project, there is little information about how to manipulate the audio tag, and tought it would be great to have a starter guide.

One thing clear is this is still an early implementation, which also means it is a bit buggy. I had a strange bug where if I had a song playing and had javascript throw errors at the same time (not related together), I could not terminate the song. Closing the tab would not stop it, I had to restart Firefox. Which lead me to think that the process is still not completely linked only to the tab (in Firefox at least).

I used jquery in my demo because the application I developing is wrapped around it, but this is not necessary.

View demo
(Only consisting of 5 buttons)

What I learned

The API is somewhat easy to work with, as it should be, play(), pause(), want to stop the volume? audioElement.volume=0. Want to start playing at 55 seconds? audioElement.currentTime = 55. Pretty cool stuff.

The default html audio player controls are added automatically with a simple: controls=”true”, it has good and bad sides. You don’t have to worry about it, but at the same time, for now, you cannot modify the player style. Also when you right click you have a nice contextual menu where you can save the file in ff3.5.

A normal audio tag

Add and play a sound via JavaScript

var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'loading.ogg');;

Get the song filepath and duration


Load a sound

var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'Mogwai2009-04-29_acidjack_t16.ogg');
audioElement.addEventListener("load", function() {; 
  $(".duration span").html(audioElement.duration);
  $(".filename span").html(audioElement.src);
}, true);

Stop a song


Change volume


Play at exactly 35 seconds in the song


The bad parts

No stop(), only pause(), I really do not know what stopped them to put stop() in the API.
No way to get the songs name.
The only accessible format are ogg and wav, no mp3 love. It also means that you will have to convert all your sounds in ogg.
As I said this is a bit buggy but understandable, the specs are not even finished.
Microsoft will probably implement the audio and video tag in 2022.

The good parts

The API is easy to use, you can have a song playing in no time.
You do not need to parse the audio object in the HTML body, you can have it played directly in your script.
In the end, I’m really happy on how this turned out.

Download the source code View demo

Tested on:
Firefox 3.5


Sign up for testking E20-322 online classes to learn the latest skills in web development. Get complete information on htlm features and functions using testking 646-276 html toturials and testking NS0-163 study guide.


Also, it’s not working in IE for me. It’s perfect in all other browsers. Any suggestions? Thanks!

by Tippie on February 10, 2012 at 4:12 pm. #

Step 1:
create a new audio object.

audio = $(”, {
‘loop’ : ‘loop’,
‘autoplay’ : ‘autoplay’

Step 2:
Than check if it can play mp3 by doing


or for ogg


Step 3:
If it can play mp3 than load the mp3, or ogg than load ogg

final code

audio = $(”, {
‘loop’ : ‘loop’,
‘autoplay’ : ‘autoplay’

if (audio[0].canPlayType(‘audio/mpeg’)){
$(”, {
‘src’ : ‘/audio/le.mp3’,
‘type’ : ‘audio/mpeg’
if (audio[0].canPlayType(‘audio/ogg’)){
$(”, {
‘src’ : ‘/audio/le.og’,
‘type’ : ‘audio/mpeg’
—– endOfCode—–

by Aras on February 13, 2012 at 4:56 am. #

Hi, the tag is working fine in Firefox for me, but on Windows the player controls look like “falling” downwards some 20 pixels after pressing play, and this causes troubles with pressing pause afterwards, because the button position is not recognised apparently. How could one make the controls stay in the same position, like it happens in Safari or Chrome, or even in Firefox on Mac OS X ?
Thank you

by nicola on February 15, 2012 at 7:00 am. #

Hello! Nice article! just one question:
What is the song you are playing in the demo? it’s quite cool!

by Gerry on March 12, 2012 at 12:15 pm. #

SoundHound tells me it’s ‘Glasgow Mega Snake’ by Mogwai. :)

by Qyzbud on June 11, 2012 at 7:43 pm. #

wow , nice ! it’s very useful for me

by 痞子 on March 29, 2012 at 11:07 pm. #

I downloaded the audiotag provided by Cedric and copied it to my website. The page is loaded like in the Demo, but the difference is that I don’t hear anything at my site. I use Firefox 6 in my Windows 7 laptop.

by Jun on March 31, 2012 at 8:14 am. #

The problem seems to be at my Goddady hosting web site. I copied audiotag to my personal web site and it works. Goddady doesn’t support java script?

by Jun on March 31, 2012 at 8:32 am. #

Wouldn’t a simple

be working as a future stop method?

by Macke on April 5, 2012 at 8:21 am. #

i do it like this:

function playsound()
var audioElement = document.getElementById(‘alarm’);;
but “document.getElementById(‘alarm’)” selector, kills jquery selectors

if i use jquery selector like this:

function playsound()
var audioElement = $(‘#alarm’);;

function playsound()

then it’s don’t work :/

how can i use it with jquery selectors ?

by MuratHan on May 26, 2012 at 5:54 pm. #

i found..
selector: $(“#audio”)[0]


function togglesound()

function stopsound()

thanks for article..

by MuratHan on May 26, 2012 at 6:46 pm. #

Thanks, this really helped me out.
Here’s the code to stop the track:
function stop() {
It’s very simple, I know, but I just thought i’d share it.

by Daniel on October 9, 2012 at 10:51 am. #

Nice work . It got me started on audio tags. i’m just curious that whether audio tag have load method or not . because the demo’s duration never shown up for me .. and it did when i changed the load to canplaythrough ..

by arun on November 1, 2012 at 7:42 am. #

Hi, only firefox and opera dont support mp3 files on js player. All the others allow mp3.

by Claudio on March 21, 2013 at 12:40 am. #

For stop
you can give like following code..

function stop()
myaudio.currentTime = 0;

by Jitendra Raj (B.Tech) on June 19, 2013 at 6:25 am. #