Brian Hadaway

Professionally Nerdy Since 2002

HTML5 Audio Player with Flash Fallback

UPDATE: If you want to use this with jQuery 1.7.1 you’ll need to change the following line in jquery.audiocontrol.js:
$player.remove(audio)
with
$player.remove("audio")
Thanks to Benjamin Desmarez at Guitar-Pro.com calling this bug to my attention.

I wanted to create a simple one-button audio player using HTML5 that would be suitable for playing samples of audio tracks. This one-button player would toggle between play, pause and loading states and would stop playing the current track when another instance of the button is clicked. Naturally, I would want to include a Flash fallback for browsers that don’t support HTML5 audio playback. I also wanted to use HTML/CSS to style a single set of controls that serve as the UI for both the HTML5 and Flash versions of the player.

What resulted from this exercise was a jQuery audiocontrol plugin that controls playback and an “invisible” Flash audio player that emulates the HTML5 <audio> element. The control lacks some necessary features such as support for playlists and “bundled” playback. This would allow users to listen to a group of tracks without continually clicking play. Also, there is currently no publicly-exposed method to kill audio playback from outside the control. This is also my first jQuery plugin so there’s probably some improvement to be made in that code. But it’s still a good basic HTML5 audio player with Flash fallback that allows you to style the buttons with CSS regardless of whether the audio is playing natively in the browser or in the Flash plugin.

The code is tested to work in the following browsers:

  1. Firefox 3.6 – Mac/PC
  2. Safari 5 – Mac
  3. Chrome 10 – Mac
  4. Internet Explorer 7+ – PC
  5. Firefox 4 – T-Mobile G2 (Android 2.2)
  6. Opera Mobile 11 – T-Mobile G2 (Android 2.2)
  7. Default Browser – T-Mobile G2 (Android 2.2)
  8. Safari Mobile – iPad and iPod Touch (iOS)

Take a look at a demo of my HTML5 audio player with Flash fallback here. Feel free to download the source or fork it on github and use it however you like. You’ll find some documentation in the jQuery plugin or you can hit me up with a comment or email if you have questions about how to customize this to your needs.

Update: If you want to see an example of the audio player in the wild check out TrumpetMoments.com. Dennis did a great job customizing the button to use on his site.

Click here to view HTML5 Audio Player with Flash Fallback »

About Brian

Brian Hadaway is a Senior Interactive Developer at Roundarch in Chicago where he specializes in front-end technologies including HTML/CSS/JS, Flash and Flex. Find him on LinkedIn, Twitter, Facebook and Foursquare.

This entry was posted in Labs and tagged , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

64 Comments

  1. Posted December 19, 2011 at 3:43 am | Permalink

    What a great little player! Just one thing…

    Like Mark above, I get an error message (‘There was an error loading this file.’) when using the Flash player and making the track autoplay with:

    $(document).ready(function(){
    $(this).audiocontrol();
    $(‘.audioButton’).click();
    });

    Any tips for using callbackFn or another method to wait until the Flash player is loaded? Sorry, not the greatest coder…

    Thanks!

  2. Posted December 19, 2011 at 10:34 pm | Permalink

    @Caroline – In jquery.audiocontrol.js find the following code…
    swfobject.embedSWF(options.flashAudioPlayerPath, options.playerContainer, "0", "0", "9.0.0", "swf/expressInstall.swf", false, false, {id:options.flashObjectID});
    and replace with something like this…
    swfobject.embedSWF(options.flashAudioPlayerPath, options.playerContainer, "0", "0", "9.0.0", "swf/expressInstall.swf", false, false, {id:options.flashObjectID}, myCallbackFunction);

    Then you’ll need to define myCallbackFunction in jquery.audiocontrol.js like this…
    function myCallbackFunction(){
    $('.audioButton').click();
    }

    I wrote this without testing so let me know if it doesn’t work.

  3. Posted December 19, 2011 at 10:41 pm | Permalink

    @Rob – Yes you should be able to point the href attribute of each li element to the load music from any folder. However I’m not sure why this is only a problem in IE.

  4. Posted December 19, 2011 at 10:44 pm | Permalink

    @Esteban – Do you get any error messages in FireFox? If you have a Firebug installed you should be able to debug the error in the console.

  5. Brian
    Posted December 24, 2011 at 4:03 pm | Permalink

    Brian .

    You use addListeners function i understand why you did that ,and this is due to flash .
    But the add addListeners function is not been support under ie9 this is why you get problem in IE,you need to find other way`s doing things and not use addListeners with the js .

  6. Posted January 8, 2012 at 7:56 am | Permalink

    Brain, I fixed my on/off-save issue using JQuery cookies.

  7. Posted January 8, 2012 at 7:06 pm | Permalink

    Nice!

  8. Abdulmohsen
    Posted January 13, 2012 at 1:00 am | Permalink

    Thank you, I have been looking for something like this for long time! :)

  9. Posted January 17, 2012 at 5:23 am | Permalink

    Hi Brian,

    This is a nice one.

    But how about IE8, there is an error message related to the .js

    Kirk

  10. Bernie C
    Posted January 23, 2012 at 2:53 pm | Permalink

    Really nice. Been looking everywhere for this sort of player. Love it! I would play for this!

  11. Jeffferson
    Posted February 5, 2012 at 10:51 pm | Permalink

    Is there a way to loop the playback?

  12. Posted February 6, 2012 at 7:03 pm | Permalink

    @Jefferson – You can use the loop attribute of the HTML5 audio tag. In jquery.audiocontrol.js Find the following line:
    audio = new Audio('');
    Immediately after it add:
    audio.loop = "loop"
    That should do it.

  13. Posted February 8, 2012 at 9:15 pm | Permalink

    This button is perfect for us! But… it doesn’t work in dynamicaly-built content. Can you tell me why my buttons don’t play? I can’t see why… paths seem right, and the css works. Could it be that my buttons are each in their own ul, with multiple ul’s on the page? Does the javascript look for certain parent elements? –> http://www.gmbc.org/sub/new_messages.php

  14. Posted February 8, 2012 at 9:32 pm | Permalink

    @Jamey – Add the class of ‘controls’ to all your ul elements and it should solve the problem. So your ul elements should look like this:
    <ul id="control-3" class="controls">

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>