I've run into a few issues while building a demo web app that plays audio files, so I wanted to post my experiences...
1: Firefox does NOT play MP3 files (as of 2013-04-17)
This is because the MPEG format is proprietary and the Firefox/Mozilla folks insist on only open source formats. You need to use .wav, .ogg or .webm. See https://developer.mozilla.org/en-US/docs/HTML/Supported_media_formats for the allowed options.
Unfortunately they do not seem to produce a useful error message if you try and play an MP3 file.
Google Chrome plays anything - so your application may appear to work fine but may fail on Firefox.
2: Firefox is VERY strict in regard to Cross-Origin Resource Sharing (CORS)
Firefox will refuse to play an audio file on a host other than the one that served the original web page. So you can't store an audio file on, say, Amazon S3, and play it on your web site. The same goes for Ajax requests to another site. The Firefox console will, or may, display an error but this is not necessarily clear.
Google Chrome doesn't seem to care about this restriction.
See this page for more details https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS
To work around this you can specify a header for the audio file which means that as the owner of the audio file you allow it to be referenced from any other site.
"Access-Control-Allow-Origin" = "*"
To work around the problem with Ajax requests you can use JSONP instead of JSON as I have described.
The special header ought to work with AWS S3... except....
3: AWS S3 does NOT allow the Access-Control-Allow-Origin header
You can set metadata headers with S3 files but not this one. The Amazon support forums are full of requests/complaints about this but it does not seem to have been resolved.
There does seem to be a way round this on Amazon Cloudfront, which pretty much exists to server assets for other sites, but from what I've seen this is pretty ugly.
My workaround for this was to add a proxy method to my server. The client requests a file that resides on S3 from the proxy. It fetches it from S3 and echoes it to the client, which sees it as coming from the originating server. This introduces an unnecessary delay but works file. Here is a snippet of code in Ruby for a Sinatra server. It uses the aws-sdk gem and leaves out set up for that.
get '/proxy' do
s3obj = bucket.objects[params['file']]
response["Access-Control-Allow-Origin"] = "*"
Apparently Google's equivalent of S3 does not have this issue - I've not tried it.
4: You cannot play audio files from localhost
This makes testing a pain. Your audio files must be hosted on a server other than localhost. I recommend Heroku as a way to get development servers up and running quickly and at no initial cost.
If you don't realize this, then there is no error message in the browser console - it simply doesn't work and you are left scratching your head until you figure it out. I hate stuff like that...
Archive of Tips
- ▼ April (5)
- ► 2012 (28)
- ► 2011 (44)
- ► 2010 (40)
- ► 2009 (53)
- ► 2008 (21)