
Using Embedded Sounds and LiveAudio
September 20, 1996
In Netscape 2.0, you could only play sounds by sending them to a helper application. Using the LiveConnect feature of Netscape 3.0 and the LiveAudio plug-in (included with Netscape 3.0) you can use sounds from JavaScript in a more transparent way. See the example for a quick look at this technique, explained fully in the rest of this article.
NOTE: This example will work only in Netscape 3.0 (beta 6 or later), and only if you have the LiveAudio plug-in installed. You'll also need a computer with sound capability (such as a PC with sound card) to try the example.
Using the <EMBED> Tag
To use a sound in your web page with LiveAudio, use the <EMBED> tag to embed the sound. LiveAudio works with the common sound formats (.wav, .au, .aif). The <EMBED> tag for a sound can include the following attributes:
- SRC is the URL of the sound file.
- NAME is a name to be used for the sound file's sound object.
- AUTOSTART can be either TRUE or FALSE; if true, the sound starts playing when the page finishes loading.
- LOOP is a Boolean value that indicates whether the sound repeats after it finishes playing. You can also specify an integer value to loop a certain number of times.
- STARTTIME and ENDTIME are the indexes within the sound file where playback will start and stop; these are in minutes and seconds; for example, 0:30 is 30 seconds from the start of the sound.
- VOLUME is the relative volume of the sound, ranging from 0 to 100.
- CONTROLS specifies the type of control, if any, displayed for the sound. The values include CONSOLE, a complete console, SMALLCONSOLE, a smaller version, and individual buttons: PLAYBUTTON, PAUSEBUTTON, STOPBUTTON, and VOLUMELEVER.
- HIDDEN can be set to TRUE to display no control at all. This is useful if you will be controlling the sound via JavaScript.
- MASTERSOUND must be used to specify that the sound file is a real sound file; LiveAudio allows "stub" files that refer to other files to be used instead.
NOTE: The MASTERSOUND attribute must be included for a sound to be controlled with JavaScript.
The Sound Object
When you embed a sound in your document with the <EMBED> tag, it is made available as a sound object, a child of the document object. The sound object's name is what you specified in the NAME attribute. The sound object has a variety of methods you can use from JavaScript:
- play() starts playing the sound. You can specify a loop value, similar to the LOOP attribute values, followed by an optional URL for the sound if the original URL is not used.
- stop() stops the sound if it is currently playing.
- pause() pauses the sound at the current position. You can use the play() method, or the pause() method again, to continue the playback.
- start_time() and end_time() allow you to override the start and end times. Specify the value in seconds.
- start_at_beginning() and stop_at_end() reset the start and stop times to the begging and end of the sound file.
- setvol() sets the sound's volume, from 0 to 100 percent.
- fade_to() sets the volume, but fades from the current value.
- fade_from_to() allows you to specify two values, and fades from one to the other.
In addition, you can use the following methods, which return a value:
- IsReady() returns TRUE if the sound is loaded and ready to play.
- IsPlaying() returns TRUE if the sound is currently playing.
- IsPaused() returns TRUE if the sound is currently paused.
- GetVolume() returns the current volume.
Example: Using Embedded Sounds
As an example of embedded sounds with the LiveAudio plug-in, I've written a quick script which plays a sound repeatedly and allows you to control it, using JavaScript event handlers. Follow this link to see this example in action or view its source.
michael moncur ... [email protected]