Make your own free website on

Class Sound

Direct Known Subclasses:

public class Sound
extends Applet

The sound class allows a JavaScript to easily specify sounds to be played and to play them in response to user interaction.

Works with:
See Also:
SoundExt SoundCtrl

Constructor Summary
Method Summary
 void play(int index)
           Called to play the sound at the given index.
 void init()
          Called by the browser or applet viewer to load the sounds. The sounds are specified by the <PARAM>s.

Constructor Detail


public Sound()
Uses the default constructor.
None directly, but see init().
Method Detail


public void play(int index)
Plays the given sound.
index - the sound's index.


public void init()
Begins loading the sounds.

It looks for parameters of the form "soundindex", with index being a number begining at 0. In other words it looks for "sound0", "sound1", "sound2", and so on. It will continue until it cannot find the next in the series (the numbers must be contiguous).

The value of the parameter is treated as the URL of the sound file. This URL must be relative to the location of the document, otherwise known as the base URL. Realize that all sound files must be 8000Hz 8bit Mono µLaw encoded .au files. There are many software packages to convert other sound files like .wav and even raw sound to .au. The best is Cool Edit.

Note that you never call this method directly but is automatically called by the browser when you set up your sounds.


The following example shows how to initilize the sounds and use them in response to events. In the intrest of protecting those with older browsers and computers, the applet is dynamicaly embedded using JavaScript. The class has been tested to work on both Netscape and IE. However, when using more than one version of script, where the script is placed in the source file can make a difference.

onMouseOver onMouseOut onMouseDown onMouseUp onClick

A sound link to nothing.


<script language="JavaScript">
  //keep older browsers from choking on sound
  function blankPlay(nothing) {}
  function blankSound() { = blankPlay;
  sound = new blankSound;
<script language="JavaScript1.2">
  //only do sounds on cool browsers
  //will work in any LiveConnected browser however
  if (navigator.javaEnabled()) {
    with(document) {
    writeln('<applet code="sound.class" name="sound" height="1" width="1">\n');
    writeln('<param name="sound0" value="">\n');
    writeln('<param name="sound1" value="">\n');
    writeln('<param name="sound2" value="">\n');
    writeln('<param name="sound3" value="">\n');
    writeln('<param name="sound4" value="">\n');

<a href="none" onMouseOver=";" onClick="return false;">onMouseOver</a>
<a href="none" onMouseOut=";" onClick="return false;">onMouseOut</a>
<a href="none" onMouseDown=";" onClick="return false;">onMouseDown</a>
<a href="none" onMouseUp=";" onClick="return false;">onMouseUp</a>
<a href="none" onClick="; return false;">onClick</a>

<a href="nothing.html" title="A link to Nowhere" onMouseOver="window.status='A link to Nowhere';; return true;" onMouseOut="window.status=''; return true;" onMouseDown="; return true;" onMouseUp="; return true;">A sound link</a> to nothing.


You must also have sound.class (1027 bytes), as well as the sounds, in the current directory (or somewhere you can get at them). To add your own sounds simply add a line,

    writeln('<param name="soundindex" value="pathToSound/">\n');

Remember that the indexs must be continuous (i.e 0,1,2,3... and not 0,1,3,4...) or else the sounds after the "hole" will not be loaded. If you try to play them nothing will happen.