[whatwg] Help with HTML5 Audio

Jeff Schiller codedread at gmail.com
Sat Sep 23 15:39:45 PDT 2006


Thanks for all the replies off the mailing list...

This my just be an education problem.  Let me ask a few questions:

Do load events bubble up?  If so, can I add a load even listener to
the document and then catch the load events there always?

If so, how can I distinguish the target is of type Audio and not of
type HTMLImageElement (or whatever), just look for e.target.play?

If my understanding is not correct above (and I will test this later),
then it seems that the spec gives me no way to guarantee that I can
attach a load event listener to the Audio object prior to the song
loading (especially for cached sounds).

Although this doesn't help me for Opera 9, I would like to propose
that the Audio constructor's argument be OPTIONAL.  If there is no
argument, then the Audio object is created without any sound loaded
and all play(), loop(), etc would just return (or return an error or
throw an exception?).  Then, add a load() method to the interface
(which would produce a load event just as the construction with an
argument would).  This also allows me to reuse the Audio object
instead of creating an Audio object for each different sound...

Thanks,
Jeff


On 9/22/06, Jeff Schiller <codedread at gmail.com> wrote:
> 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