[whatwg] Help with HTML5 Audio

Jeff Schiller codedread at gmail.com
Fri Sep 22 14:04:27 PDT 2006


Opera 9 implements the Audio interface proposed in HTML5:
http://whatwg.org/specs/web-apps/current-work/#scs-sound

I'm having trouble with it. My first attempt was:

var soundClip = new Audio("blah.wav");
if(soundClip) { soundClip.play(); }

This doesn't work on the first attempt, but on the second attempt it
plays the sound just fine. The only thing I could deduce was that the
first execution, the .play() lines was happening before the .wav file
finished loading from the server. But once the file is loaded from the
web server, subsequent invocations of this script mean the file has
cached, so the second line is happening after the load event.

Sure enough, the HTML5 spec talks about the "Audio object that will,
at the completion of the current script, start loading that URI" and
"Once the URI is loaded, a load event must be fired on the Audio
object"

So this was my second attempt:

function playSound(evt) {
if(evt && evt.target && evt.target.play) { evt.target.play(); }
}
...
var soundClip = new Audio("blah.wav");
if(soundClip) { soundClip.addEventListener("load", playSound, true); }

However, this doesn't seem to work at all (no matter how many times I
call it). So I'm not sure what I'm doing wrong. Is it possible that
I'm trying to add the event listener after the sound loads (in which
case the Load event already fires)? If so, how can I get around this?

I need a solution that will work whether the sound is cached or not.
Is this a problem in the spec or a problem with my understanding of
this object or event handlers?

Thanks,
Jeff


More information about the whatwg mailing list