Skip to navigation

PCPro-Computing in the Real World Printed from www.pcpro.co.uk

Register to receive our regular email newsletter at http://www.pcpro.co.uk/registration.

The newsletter contains links to our latest PC news, product reviews, features and how-to guides, plus special offers and competitions.

// Home / Blogs

Posted on August 23rd, 2010 by Ian Devlin

Adding audio to your website with HTML5

old gramaphone
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:

  • auto – loads the entire audio file when the page loads
  • meta – only load the audio files meta data when the page loads
  • none – don’t load the audio file when the page loads
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!

Tags: ,

Posted in: How To

Permalink

Follow any responses to this entry through the RSS 2.0 feed.

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 ”

  1. M Viracca Says:
    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.

     
  2. David Wright Says:
    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…

     
  3. Ian Devlin Says:
    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.

     
  4. David Wright Says:
    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.

     
  5. Darkharedlord Says:
    August 25th, 2010 at 11:18 am

    nothing makes me click the little X in the corner quicker than music on a website…

     

Leave a Reply

Spam Protection by WP-SpamFree

* required fields

* Will not be published

SEARCH
SIGN UP

Your email:

Your password:

remember me

advertisement


Hitwise Top 10 Website 2010