views:

928

answers:

3

Right now I'm trying to add a button to a webpage that when pressed will start playing a .wav file by dynamically adding an embed tag to the DOM. The text switches to "Stop" while the sound plays, and I'd like to be able to switch it back automatically to "Play" once the file has finished playing.

How would I be able to execute some Javascript automatically when the file is finished?

Thanks.

A: 

If you are embedding a player to play the file (as opposed to just allowing the browser to try to figure what to use on its own (QT, WMP etc.)) then the player should have its own events you can hook into. That depends on the player you use.

Diodeus
+1  A: 

I would wrap the sound in a very simple flash movie, and use actionscript to trigger a javascript function at the end.

Here is a list of open-source flash ide : http://osflash.org/open_source_flash_projects

(That's how google did it in gmail)

BraveSirFoobar
+1  A: 

Use jPlayer for jQuery, it has an event that triggers when the sound has finished playing.

Requires client-side Flash, but the first Flash players works even when my Firefox Flash-blocker is enabled.

They have several demo players already built.

Sire
it's probably worth pointing out that jPlayer essentially inserts a flash object into the page to play the sound, as suggested by BraveSirFoobar.
Chris