On Monday, we are planning on revealing an interactive graphic that uses audio as a way to explain some information used within itself. But in order to do so, we needed a suitable solution of being able to play that sound across a wide range of browsers (Firefox 3.5+, IE7+, Safari, Chrome, Opera, iOS). I knew existing libraries out there existed (audio.js, SoundJS) that cover the same browser spectrum, but honestly, if there’s a chance for me to learn about something. I would prefer to.
So I dove in. Head first.
This is what the plan was:
- Test if the browser supported HTML5 audio be it mp3 or ogg. If it did, create an audio element and use that for the graphic.
- If it failed that test, check if the browser has flash as this is going to be the only other way of providing the audio.
- If it fails the flash test, tell them to go away.
I had done some previous research into HTML5 audio and what are the best audio formats so I had this nice table:
I think my information is pretty much taken from here, so give it a read if you’re curious.
What I learnt first - HTML5 can be blunt
Testing a browser for HTML5 audio can consist of two tests: checking if the browser can play a certain file format and then actually trying to play the file format. The reason behind this is that whoever made the method for checking formats, made it’s response quite blunt.
The code to test whether a browser can play a format consists of this:
However! It’s response are probably, maybe or no (or just an empty string). Now to me, probably and maybe, are pretty much the same thing.
“Are you going out tonight?”
“Probably” / “Maybe” (In my case, no)
Anyway, what I read is that it is best to go with the assumption that the browser can play the audio if it says probably. But if it says maybe, you’re going to need to check.
This allows us to attempt to load the audio, play it and then pause it. If it successfully loads the audio, we are good. If it catches an error, we are going to need to try something else. I sit somewhere on the fence for choosing the correct listener as you can pick canplay, canplaythrough and loadeddata. loadeddata seemed to always work for me so I stuck with it.
What I learnt next - iOS is a little different
iOS does not like to fire off canplay, canplaythrough or loadeddata when trying audio. Instead, it fires off progress. So we need to do a switch if it happens to be an iOS device.
One issue with iOS is that you cannot play audio instantly without the user having some sort of input (which I suppose is fair as I wouldn’t want sound just playing suddenly when I go on a page (however there was at one point, audio playing instantly when I loaded my test page. I’m not sure what happened)). So this means that the device will fail the play and pause methods meaning the only progress event to be fired will be the load if it is successful.
Now we are about to enter Flash territory.
Flash problem numero uno - security
I knew that one of the issues would be cross-domain scripting so I had this perfectly placed as the first thing Flash should do:
But alas, nothing would communicate with each other. To make an insanely long story short, if you are doing this sort of thing, have it on a server. Don’t just load the page in your browser as that will NOT work.
Flash problem numero dos - classid
I don’t use Flash often. Actually, I don’t use Flash. However I have seen enough example code of the object and embed tags and what is usually included attribute and parameter wise to know what to include. But if you don’t include the classid attribute on the object tag and despite Flash is still working fine in Chrome, Safari, Firefox and Opera (I think) but not in Internet Explorer, you won’t think that is a problem. Well guess what boys and girls, it does. I spent a good few hours going over if I was missing something that IE needs not thinking it was this attribute (especially as I read that IE only uses the embed tag, not the object). It wasn’t till I actually read the spec of the tag for Flash on Adobe that it mentions classid is a must that I put it on.
Flash problem numero tres - dynamically loading Flash into the page
I went through several stages of trying to load flash into a page.
All of these examples worked in at least a few of them but none, worked in Internet Explorer. This was a kick in the nuts as my intention was to use Flash as the fallback for IE7 and 8. The problem that was occuring was that the ExternalInterface callbacks were not being added which led to me not being able to call them and when I closed the page, it threw errors trying to remove something that had not been added. So I decided to go sniffing and poked around audio.js’ source where I came across this:
What you need to do is create a div element and set it’s innerHTML to all the object and embed code and then add it to the page through innerHTML rather than appendChild. Honestly though, I’m not too sure why this work so if anyone happens to read this and knows why, I would like to know.
Flash problem numero cuatro - getMovie
There’s a common piece of code floating around used to get the Flash object consisting of:
It’s great as it works in the majority of browsers but not in IE9. This is due to IE9 now including the embed tag element in the DOM. So you then need to adjust the code a tiny bit so it’s not including IE9 in the navigator.appName check.
I’ve just had a quick look back over this post and realised it is fairly lengthy. However I hope this might help solve other people’s problems if they come across certain issues when creating a cross-browser audio plugin/library through HTML5 audio and Flash.