Pages

Tuesday, August 9, 2011

Quick Guide to Implementing the HTML5 Audio Tag

Learn how to use the HTML5 audio tag and fall back to Flash on platforms which do not support it.

Step 1: Wrap your Flash object with the audio tag
Those browsers that don't recognize the audio tag will load the Flash content instead.
<audio>

    <object class="playerpreview" type="application/x-shockwave-flash" 
            data="player_mp3_mini.swf" width="200" height="20">
      <param name="movie" value="player_mp3_mini.swf" />
      <param name="bgcolor" value="#085c68" />
      <param name="FlashVars" value="mp3=test.mp3" />
      <embed href="player_mp3_mini.swf" bgcolor="#085c68" width="200" 
             height="20" name="movie" align="" 
             type="application/x-shockwave-flash" flashvars="mp3=test.mp3">
      embed>
    object>

audio>

Step 2: Add the source reference

We can add as many "source" lines and formats as we want. If the browser doesn't support one specific format it will fallback to the next one and so forth.
<audio>
  <source src="test.mp3" type="audio/mpeg" />
  <source src="test.ogg" type="audio/ogg" />
    
  <object class="playerpreview" type="application/x-shockwave-flash" 
          data="player_mp3_mini.swf" width="200" height="20">
    <param name="movie" value="player_mp3_mini.swf" />
    <param name="bgcolor" value="#085c68" />
    <param name="FlashVars" value="mp3=test.mp3" />
    <embed href="player_mp3_mini.swf" bgcolor="#085c68" width="200" 
           height="20" name="movie" align="" 
           type="application/x-shockwave-flash" flashvars="mp3=test.mp3">
    embed>
  object>
    audio>

Step 3: Add fallback to Flash

To be safe, we need to add the fallback to a Flash audio player, in case the browser doesn't support any of the formats we specified. For instance, Firefox 3.5 only supports the audio tag with Ogg format, but we might only have the mp3file available.
Note: There are also tools and online converters you can use if you want to create ogg files from your mp3 and add support for ogg too.
<audio>
    <source src="test.mp3" type="audio/mpeg" />
  
  <object class="playerpreview" type="application/x-shockwave-flash" 
          data="player_mp3_mini.swf" width="200" height="20">
    <param name="movie" value="player_mp3_mini.swf" />
    <param name="bgcolor" value="#085c68" />
    <param name="FlashVars" value="mp3=test.mp3" />
    <embed href="player_mp3_mini.swf" bgcolor="#085c68" width="200" 
           height="20" name="movie" align="" 
           type="application/x-shockwave-flash" flashvars="mp3=test.mp3">
    embed>
  object>
    audio>

<div id="player_fallback">div>
<script>
  if (document.createElement('audio').canPlayType) {
    if (!document.createElement('audio').canPlayType('audio/mpeg')) {
      swfobject.embedSWF(
        "player_mp3_mini.swf", 
        "player_fallback", 
        "200", 
        "20", 
        "9.0.0", 
        "", 
        {"mp3":"test.mp3"}, 
        {"bgcolor":"#085c68"});
    }
  }
script>
To make it easier, we are using the SWFObject library to insert the Flash player via JavaScript. To include the library you can simply use the Google AJAX Libraries API inserting these two lines in your header:
<script src="http://www.google.com/jsapi">script>
<script>google.load("swfobject", "2.2");script>

Step 4: Add the default controls to show the player

These controls are not customizable (see examples at the end). Since these default controls will show up regardless of the supported format we will need to handle its visibility with the conditional we previously created.
<audio id="audio_with_controls" controls>
  <source src="test.mp3" type="audio/mpeg" />
    
  <object class="playerpreview" type="application/x-shockwave-flash" 
          data="player_mp3_mini.swf" width="200" height="20">
    <param name="movie" value="player_mp3_mini.swf" />
    <param name="bgcolor" value="#085c68" />
    <param name="FlashVars" value="mp3=test.mp3" />
    <embed href="player_mp3_mini.swf" bgcolor="#085c68" width="200" 
           height="20" name="movie" align="" 
           type="application/x-shockwave-flash" flashvars="mp3=test.mp3">
    embed>
  object>
    audio>

<div id="player_fallback">div>
<script>
  if (document.createElement('audio').canPlayType) {
    if (!document.createElement('audio').canPlayType('audio/mpeg')) {
      ... SWFObject script line here ...
      document.getElementsById('audio_with_controls').style.display = 'none';
    }
  }
script>
Alternatively, you can create your own player using JavaScript and CSS.
<audio id="audio">
  <source src="test.mp3" type="audio/mpeg" />

  <object class="playerpreview" type="application/x-shockwave-flash" 
          data="player_mp3_mini.swf" width="200" height="20">
    <param name="movie" value="player_mp3_mini.swf" />
    <param name="bgcolor" value="#085c68" />
    <param name="FlashVars" value="mp3=test.mp3" />
    <embed href="player_mp3_mini.swf" bgcolor="#085c68" width="200" 
           height="20" name="movie" align="" 
           type="application/x-shockwave-flash" flashvars="mp3=test.mp3">
    embed>
  object>
    audio>

<div id="player_fallback">div>
<div id="player" style="display: none">
  <button onClick="document.getElementById('audio').play()">Playbutton>
  <button onClick="document.getElementById('audio').pause()">Pausebutton>
div>
<script>
  if (document.createElement('audio').canPlayType) {
    if (!document.createElement('audio').canPlayType('audio/mpeg')) {
      ... SWFObject script lines here ...
    } else { // HTML5 audio + mp3 support
      document.getElementById('player').style.display = 'block';
    }
  }
script>

Examples

The following two examples will fallback to the Flash player in those browsers that don't support the audio tag nor can play mp3 in it.

Player with default controls


Player with customized controls

 
If you don't want to start your customized player from the scratch you can take a basic one and style it from there.
You are all set!