W3C home > Mailing lists > Public > whatwg@whatwg.org > September 2006

[whatwg] Help with HTML5 Audio

From: Jeff Schiller <codedread@gmail.com>
Date: Fri, 22 Sep 2006 16:04:27 -0500
Message-ID: <da131fde0609221404n1e1da1a9gc168015a1b3f16c0@mail.gmail.com>
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
Received on Friday, 22 September 2006 14:04:27 UTC

This archive was generated by hypermail 2.3.1 : Monday, 13 April 2015 23:08:29 UTC