Monday 11 June 2012

How to play HTML5 audio through JavaScript

A simple memo of how to play HTML5 audio using JavaScript

var audioElement = new Audio();
audioElement.src = 'http://www.somesite.com/audioresource.ogg';
audioElement.load();
audioElement.play();
That's all. Is that simple :D

Note that some browser do not supports ogg formats.
You might want to encode that specified format to support playback for that particular browser.
Well, I don't care about those browsers though.

As for mobile browsers... (MobileSafari, Android Browser)
Unfortunately they do not work as planned.

Reference :
http://www.phoboslab.org/log/2011/03/the-state-of-html5-audio
http://remysharp.com/2010/12/23/audio-sprites/
http://24ways.org/2010/the-state-of-html5-audio

It's year 2012 and Apple haven't solve that problem yet...
I do not owned an Android Device, I do not know is it fixed or not...

No comments:

Post a Comment