Posted on August 23rd, 2010 by Ian Devlin
Adding audio to your website with HTML5

With all the furore around the HTML5 video element, the poor audio element sits in the background wondering what it’s done wrong to receive so little attention. So in an attempt to redress the balance, I’m going to show you how you can stream audio to your website visitors without any additional plugins.
As with the video element, in the past the only method of embedding audio files into a web page was to use Flash or another third-party plugin. There simply was no other way. With the introduction of the audio element, this has changed.
Browsers and file types
There are currently three different audio file types that are supported, and (of course) the different browsers each support different formats. The table below illustrates this:
| Vorbis OGG | MP3 | WAV | |
|---|---|---|---|
| Firefox 3.6.8 | ✓ | x | ✓ |
| Chrome 5 | ✓ | ✓ | x |
| Safari 5 | x | ✓ | ✓ |
| Opera 10.6 | ✓ | x | ✓ |
| Internet Explorer 8 | x | x | x |
| Internet Explorer 9 | x | ✓ | x |
| iPhone | x | ✓ | ✓ |
| Android | x | x | x |
Code
So how do we actually go about adding an audio file to a web page? Well it’s quite simple really, especially if you’ve already used the video element.
Instead of using the <video> element we naturally use its brother, the <audio> element. Like the <video> element, we can use the src attribute to directly specify the audio file to play, but, as shown above, it’s more practical to use the <source> element to indicate a number of possible sources in order to cover all most browsers.
The <audio> element
The audio element takes the following attributes:
| Attribute | Description |
|---|---|
| src | a valid URL to the audio file itself |
| preload | Specifies if the audio is to be preloaded or not. Possible values:
|
| autoplay | a boolean indicating whether the audio file should be played automatically |
| controls | a boolean indicating that the default media controls should be displayed by the browser |
| loop | a boolean indicating whether the audio file should be played repeatedly (this is evil) |
So you can easily add a WAV audio file to your website by using the following markup:
<audio src="audioFile.wav" autoplay controls></audio>
Of course, as mentioned above, this can limit your browser compatibility and it’s better to provide different audio sources using the <source> element.
The <source> element
The source element can take the following attributes:
| Attribute | Description |
|---|---|
| src | a valid URL to the media (in this case audio) file itself |
| type | the type of the media file which must be a MIME type, e.g. type="audio/ogg" indicates that it is a Vorbis OGG audio file, and you can also provide the MIME codec to help the browser to decide how to play the audio by using type='audio/ogg; codecs="vorbis". |
Stacking the audio sources
Combining the <audio> and <source> elments together couldn’t be simpler, as the code below shows:
<audio controls autobuffer>
<source src="audioFile.ogg">
<source src="audioFile.mp3">
<source src="audioFile.wav">
Sorry, your browser does not support the audio element
</audio>
The code above should play the specified audio file in all of the browsers mentioned above (except for the current incarnations of Internet Explorer and Android – which will display the message indicating as much. This could be replaced with a Flash fallback of course).
Feel free to test this code for yourself.
Conclusion
So there it is in its simplest form, adding an audio file to a web page. Of course it depends on the browser the page is being viewed in, and, as usual, we’re still waiting for Internet Explorer to catch up (A beta version of IE9 is due out next month). Nevertheless, it’s still worth using today. Just don’t set your files to autoplay!
You can skip to the end and leave a response. Pinging is currently not allowed.
5 Responses to “ Adding audio to your website with HTML5 ”
Leave a Reply
Authors
- Barry Collins
- Chris Brennan
- Christine Horton
- Darien Graham-Smith
- Dave Stevenson
- Davey Winder
- David Bayon
- David Fearon
- Ewen Rankin
- Ian Devlin
- Jon Honeyball
- Jonathan Bray
- Kevin Partner
- Mike Jennings
- Nicole Kobie
- Sasha Muller
- Steve Cassidy
- Stewart Mitchell
- Stuart Turton
- Tim Danton
- Tom Arah
Categories
- About the bloggers
- Android App of the Week
- cloud computing
- Green
- Hardware
- How To
- iPhone App of the Week
- Just in
- Microsoft Office 2010
- Newsdesk
- Online business
- Random
- Rant
- Real World Computing
- Software
- View from the Labs
- Windows 7
- Windows 8
Archives
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
advertisement


August 23rd, 2010 at 11:14 am
Firefox and Opera do not support MP3s? Android doesn’t support anything?! I’m guessing they’re working on this, at least, I hope so.
August 23rd, 2010 at 12:19 pm
I doubt Firefox will support MP3, it isn’t an open source codec, so they’d have to pay for a licence, and they wouldn’t be able to release the codec as part of the standard Firefox package, because it would violate the open source licence. Users would have to download it as an extra add-on.
Conversely, Ogg is open source, but is guaranteed not to break any commercial patents (same as the VP8 and Ogg-Theora video codecs), so large companies aren’t keen on using them, because they can’t by a licence and protect themselves from potential litigation…
August 23rd, 2010 at 1:46 pm
Many thanks to HTML5 Guru Bruce Lawson for pointing out that autobuffer has been replaced by preload and that I clearly meant Vorbis OGG and not Theora (which is a video codec). I’ve fixed these in the article.
August 24th, 2010 at 6:43 am
Bleh! Just seen I missed out a NOT in my last post…
I should read:
Conversely, Ogg is open source, bit is NOT guaranteed not to break any commercial patens (same as the VP8 (Google) and Ogg-Theora video codecs), so large companies aren’t keen on using them, because they can’t buy a licence and protect themselves from potential litigation.
August 25th, 2010 at 11:18 am
nothing makes me click the little X in the corner quicker than music on a website…