Audio Controls (for Netscape 3,4, and IE 4)

Warning: If you are using IE4 or IE5, and you have installed Microsoft Media Player, the following code won't work. This is because Microsoft was kind enough to break the compatibility of IE's multimedia controls (on purpose perhaps?). I haven't bothered to comb through the latest IE documentation for how they now want people to do audio controls. Also beware, some later versions of Netscape seem to screw up when checking for the LiveConnect plugin - it's there, but for some reason you still get errors. I'll eventually get back to this aspect of JavaScript and clean everything up. Anyone want to help?

Controlling audio is quite simple once you know what the commands are. The easiest way I've found to do it is by using the EMBED tag to load the audio file (wav, au, midi etc.). Then using the appropriate commands you can either play or stop the file.

So first, here's the embed tag (it's pretty self-explanitory):

<EMBED NAME="myaudio" SRC="myaudio.mid" LOOP=FALSE AUTOSTART=FALSE HIDDEN=TRUE MASTERSOUND>

The code to make Netscape 3 or Netscape 4 play this file is:

document.myaudio.play()

If you want the file to loop, you have to use .play(true)

And the code for Internet Explorer 4 is very similar:

document.myaudio.run()

In IE the file will loop depending on the LOOP property in the EMBED tag.

Finally, the code to stop playing the file is the same across all the browsers:

document.myaudio.stop()

View audio1.html for an example (this example only works if your browser is properly configured for audio playback)

Audio Error Checking

There's a whole slew of things to consider when implementing audio into your page. Not all versions of Netscape 3 and 4 have audio capabilities, and they'll give an error when it reads the EMBED tag. And if the files haven't loaded yet and you try to play them, they'll give some other error. And if you use IE 4 and try to start a file while another one is playing or if you execute an audio command from an HREF tag it won't do anything,... blah, blah, blah.

But don't worry, on the following example I've done most of the error checking you could possibly need, and you'll probably be able to just cut and paste what you need. I've included a lot of remarks to explain what I'm doing, so I think you'll be able to follow through it.

View audio2.html for an example with multiple sounds and audio error checking. View Source Code

Home Next Lesson: Layer Writing
copyright 1998 Dan Steinman


Casa de Bender