« Back to blog

Getting HTML5 Audio tag and flash fallback to work nicely with all browsers

In implementing the new (yet-to-be-released) html Ambiance audio pages, I wanted to take advantage of modern browsers support of the tag. I loaded it up with a flash fallback but for some reason, in Firefox (!!!), mp3's cannot be played with html5 audio. Ambiance uses mp3 files and caf files, neither of which are supported by Firefox and html5.

I was left with 2 choices:

  1. Convert the entire Ambiance library to ogg just for firefox users.
  2. Fine tune the javascript to properly fall back onto the flash AudioPlayer

I decided that ogg sucks and I would not waste any time doing the conversions, so I went for number 2.

Using code from html5doctor as a guide, and using the Standalone Flash WPAudioPlayer, I came up with the following code snippet that seems to work in all browsers.

It pretty much speaks for itself, but here is quick explanation. First, the html5 audio tag is placed on the page with the id="audioplayer". Next, some javascript runs that checks a few things and if any of them fail, it replaces the id="audioplayer" element with the fallback flash player. Of the things it checks, it checks that the browser can create the audio element (html5 supported), then it check that the browser can play a filetype of audio/mpeg (mp3). If any of these fail, it will go to flash. This works on all the browsers I have tested on, including firefox, but I make no guarantees. Just from code inspection, if the user has javascript disabled, it won't work. But I don't care. Who really wants to provide support for those (L)users anyway ;)

Hope it helps.

| Viewed
times | Favorited 0 times

45 Comments

Jun 18, 2010
Hobbes said...
Thx! very helpul and nicely codded.
Is it ok to re-post it on my code blog? so more people will find it (with credits, of course!)

thx a lot.

Aug 30, 2010
Adam Allaway said...
I've stumbled upon this from google whilst looking for html5 audio with flash fallback. I have a question though:

When I look at your standalone player link it states it's for wordpress. Does this mean your whole solution is also just for wordpress based sites?

Aug 30, 2010
Matt Coneybeare said...
Thanks for the question. "Standalone" means it does not need wordpress to run.

Sep 13, 2010
dg said...
thanks for posting this. i am curious where you insert this code so the player will work on iphones and ipads. any thoughts?
Sep 13, 2010
Matt Coneybeare said...
The code is html, so it is on a webpage somewhere. the iPhone and iPad can see it in Safari, like any other webpage, or developers can open an html page using UIWebView inside an iphone app.
Dec 28, 2010
rick james said...
this is a great little mod.

thanks for the work.
for those of you who found this site without knowledge of the wordpress stand alone player, be sure to start with that, then look at this code to create a cross platform / cross browser player.

again, very nice. thank you.

Mar 18, 2011
Lloyd Watts said...
Hi,
Thank you for posting this. It is working very well for me on Safari on my Windows 7 machine, and also on my iPhone. But surprisingly, it is not working on Internet Explorer. Sorry for the dumb question, but have you verified correct fallback to flash on IE? Or perhaps I am doing something wrong?

Here is a link where I am trying to use it. The code is embedded several times to display a set of mp3 files at the bottom of the page.
http://www.lloydwatts.com/arts_music.html

I'm trying to use it to replace the Flash playlist.com player, which does not work on iPhone, and also doesn't work outside the U.S. I do have javascript enabled.

Any help you might give would be much appreciated.

Thank you,
Lloyd

Mar 18, 2011
Matt Coneybeare said...
I just tested your site in IE 8 and it is working for me, in that it falls back to the flash player. At the time this post came out, I am pretty sure I tested on all the browsers but to be honest, I cannot remember exactly which ones.
Mar 18, 2011
Lloyd Watts said...
<tr><td>

Thanks for the quick reply.
I realized that the Standalone solution still required the .js files, and that got it working.
Thank you,
Lloyd


---
</td></tr>
Apr 17, 2011
hannes said...
hi. i like your script - how can i solve it to use mp3 and ogg where possible in the browser and only if those 2 formats are not supported, i falllback to flash ?
Apr 17, 2011
Matt Coneybeare said...
Just replace the .caf file with a .ogg file

Apr 17, 2011
john said...
too bad that you don't like/support the open-source ogg audio format ..
but I don't wanna troll here eheh
thx for sharing this fallback anyway !!
Apr 24, 2011
Joshi said...
Thanks for this!
I am very much a beginner with this kind of stuff, so please don't laugh ;)
my audio file is at http://brainfood.lu/podcast/files/podcast_31.mp3
what is then wrong with this code? (it won't play)

<audio>
    <source src="http%3A//brainfood.lu/podcast/files/podcast_31.mp3">
</audio>

Thanks!

Apr 24, 2011
Matt Coneybeare said...
It looks like your src has a %3a in it instead of a colon.
Apr 24, 2011
Joshi said...
thanks for your relpy.
"<audio>
<source />
</audio>
" won't work either...
Apr 24, 2011
Joshi said...
also, I have no idea why I cannot post the entire code here... it's on here: http://www.brainfood.lu/
Apr 24, 2011
Matt Coneybeare said...
On that page you have 2 sets of quotes around the URL.
Apr 24, 2011
Joshi said...
Oh, thanks, that seems to work in chrome and safari! (I'm sorry to bug, but why doesn't it work with the flash fallback in firefox?) :)
Apr 24, 2011
Matt Coneybeare said...
I don't see any of the code for the flash player on your page at all. Make sure you set it up correctly: http://wpaudioplayer.com/standalone/
Apr 24, 2011
Joshi said...
Oh ok! Sorry. I've tried setting it up, uploading the javascipt and the swf file, and adding
... to my header. And still. If I ever get this to work, I'll buy all of your apps.
Jun 04, 2011
tcrxt said...
ogg does /not/ suck.
It's about freedom, never heard of that?
Jun 04, 2011
Matt Coneybeare said...
@tcrxt Freedom is great, but as a professional… full compatibility across every platform is better.
Oct 06, 2011
Brad said...
I've got the Wordpress-installed version of WPAudioPlayer and when I add the code above I only get the HTML5 player.

It's broken because I only have an mp3 in the code. I assumed it would fallback to the WPAudioPlayer player but it didn't.

Is there something I've got to tweek since it's not the stand alone player?

Oct 06, 2011
Matt Coneybeare said...
@Brad I am not familiar with Wordpress at all but it shouldn't be failing with only 1 audio source.
Oct 27, 2011
Pekka M said...
It does not work in iphone or ipad. There is no player and no sound.... I have mp3 and m4a-files of audio:
<audio>
<source src="audio/rosa.mp3" type="audio/mp3">
<source src="audio/rosa.m4a" type="audio/mpeg">
</audio>
Oct 27, 2011
Matt Coneybeare said...
@Pekka it does indeed work on iOS devices. Open the following url on your device to see it in action. http://ambianceapp.com/584 Ensure you have the paths setup correctly.
Oct 27, 2011
Pekka M said...
My sound work in every other browser and also in Android, but not in iphone.??
Oct 27, 2011
Matt Coneybeare said...
@Pekka try using absolute paths instead of relative ones… i.e. http://site.com/audio/rosa.mp3
Oct 27, 2011
Pekka M said...
What fileformat the file must be, that it works in iphone? But I cant see the player neither. All other browsers in normal computers show the player.
Oct 27, 2011
Pekka M said...
I tried those absolut paths, not working...
Oct 27, 2011
Matt Coneybeare said...
@pekka Why don't you post your url and I can take a look if i have time
Oct 27, 2011
Pekka M said...
The site: http://www.diamondo.fi
Oct 27, 2011
Matt Coneybeare said...
@Pekka you have not setup the code correctly. I suggest rereading the above code. iOS cannot load flash. This post shows how to make an html5 player first, with a flash fallback. You just have the flash fallback because you are loading the flash player at page load instead of in the audio fallback
Oct 28, 2011
Pekka M said...
Whati is excatly wrong in my code?
Oct 28, 2011
Matt Coneybeare said...
@Pekka Contact me directly ( http://coneybeare.net/#contact ) and you can hire me for a nominal fee to solve your problem. Otherwise, I would suggest removing all of your existing code relating to the audio player, and follow the directions in this post again.
Oct 28, 2011
Pekka M said...
Ok, i thought that this was so called open forum, I'll study those things myssellf, if you want payment...
Oct 29, 2011
Pekka M said...
I changed the code: www.diamondo.fi and now it works also in iphone, but not in IE 8... Works in all other browsers...
Nov 24, 2011
Jeremy said...
Hey Matt - great work here. Any way you can tweak the code to play audio on the click of a link, with the flash fallback? I've always liked NiftyPlayer http://www.varal.org/niftyplayer/, but I think it's about time I start with HTML5.
Dec 05, 2011
Mihai P said...
Hy,

I can't make my .caf files to work.
Can you please explain to me what lines of code should i use ?
All the plugins and stuff that i tried are not working for .caf files.

Thank you.

Jan 04, 2012
suresh said...
Does it support wav? and i have tried this code on HTTPS web site it deost work on safari.
Jan 04, 2012
Matt Coneybeare said...
Yes, the html5 audio tag supports wav. Are you sure you have all the paths setup correctly?

Jan 04, 2012
suresh said...
Yes, all paths are correct as i was able to play using HTTP channel. We have issue with only HTTPS.
Jan 04, 2012
Matt Coneybeare said...
If you only have issues with https, I doubt it is the audio tag that is tripping you up. I would look elsewhere for the issue, or consult the html5 specs as I don;t know why it might be failing to play wav's for you.

Jan 06, 2012
roozbeh15 said...
Hey, this is amazing. Great job and thank you. Any chance I could read the remaining time of the .mp3 file on the js side? That's the only functionality that I need and this solution will be Golden!!! :) Any help would be much appreciated.
Jan 09, 2012
roozbeh15 said...
I ended up writing my own flash mp3 player. I have more control over things this way. But thanks for sharing this anyways!

Leave a comment...