soundCtrl
Class SoundCtrl

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Container
              |
              +--java.awt.Panel
                    |
                    +--java.awt.Applet
                          |
                          +--SoundCtrl
Direct Known Subclasses:
None.

public class SoundCtrl
extends Applet

The sound class allows a JavaScript to easily specify sounds to be played and to play them in response to user interaction. As well as the capability to loop and stop sounds, it can also be turned on and off and the startup state (on or off) specified. However, the extra few bytes are not always needed.

Works with:
JDK1.0
See Also:
Sound SoundExt

Constructor Summary
SoundCtrl()
 
 
Method Summary
 void on()
           Enables sound playing.
 void off()
           Disables sound playing until on is called.
 boolean isOn()
           Reports if sounds can be played.
 void play(int index)
           Called to play the sound at the given index.
 void loop(int index)
           Called to begin looping the sound at the given index.
 void stop(int index)
           Called to stop 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

SoundCtrl

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

on

public void on()
Enables sound playing for this soundCtrl object.

off

public void off()
Disables sound playing for this soundCtrl object. It does this by calling stop() for each sound and ignoring any requests to play() or loop() a sound until on() is called.

isOn

public bolean isOn()
Returns true if sound playing by this object is enabled (parameter "sound" has value "on" or on() called). Returns false if it is disabled (parameter "sound" has value other than "on" or off() called).

play

public void play(int index)
Plays the given sound. If for some reason there is some problem and the sound cannot be played, nothing happens. The sound is not played if the sounds for this object are currently off().
Parameters:
index - the sound's index.

loop

public void loop(int index)
Begins to loop the given sound. Note that this method does not thread the loop, but just loops it. This can make other sound functions slow. Also, it may not stop opon unloading the page as the applet does not release anything explicitly and relies on the garbage collector. This means that in many cases the sound will continue to play while loading another page and then continue playing on top of that page. Care must be taken to not have too many instances of this class running at the same time when using loops.

If for some reason there is some problem and the sound cannot be played, nothing happens. The sound is not played if the sounds for this object are currently off().

Parameters:
index - the sound's index.

stop

public void stop(int index)
Stops the given sound. If for some reason the sound cannot be stoped, nothing happens. This may happen if the sound does not exist or is not currently playing.
Parameters:
index - the sound's index.

init

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.

Init() also looks for a parameter with the name "sound". If the value of this parameter is "on" (case insensitive), sounds may be played as soon as they download. If the value is anything else (i.e. "off") or the parameter "sound" ommited, sounds will be off by default and will not play until on() is called.

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

Example

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

Begin Loop End Loop

A sound link to nothing.

Sound On Sound Off

Source:

<!-- make some aural possibilities -->
<script language="JavaScript">
<!--
  //keep older browsers from choking on sound
  function blankPlay(nothing) {}
  function blankStop(nothing) {}
  function blankLoop(nothing) {}
  function blankOn() {}
  function blankOff() {}
  function blankSound() {
    this.play = blankPlay;
    this.stop = blankStop;
    this.loop = blankLoop;
    this.on = blankOn;
    this.off = blankOff;
  }
  sound = new blankSound;
//-->
</script>
<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="soundctrl.class" name="sound" height="1" width="1">\n');
    writeln('<param name="sound0" value="c4.au">\n');
    writeln('<param name="sound1" value="d4.au">\n');
    writeln('<param name="sound2" value="e4.au">\n');
    writeln('<param name="sound3" value="f4.au">\n');
    writeln('<param name="sound4" value="g4.au">\n');
    writeln('<param name="sound5" value="looprockboat.au">\n');
    writeln('<param name="sound" value="on">'\n);
    writeln('</applet>');
    }
  }
//-->
</script>

<a href="none" onMouseOver="sound.play(0);" onClick="return false;">onMouseOver</a>
<a href="none" onMouseOut="sound.play(1);" onClick="return false;">onMouseOut</a>
<a href="none" onMouseDown="sound.play(2);" onClick="return false;">onMouseDown</a>
<a href="none" onMouseUp="sound.play(3);" onClick="return false;">onMouseUp</a>
<a href="none" onClick="sound.play(4); return false;">onClick</a>

<p>
<a href="none" onClick="sound.loop(5); return false;">Begin Loop</a>
<a href="none" onClick="sound.stop(5); return false;">End Loop</a>

<p>
<a href="nothing.html" title="A link to Nowhere" onMouseOver="window.status='A link to Nowhere'; sound.loop(5); return true;" onMouseOut="window.status=''; sound.stop(5); return true;" onMouseDown="sound.play(1); return true;" onMouseUp="sound.play(2); sound.stop(5); return true;">A sound link</a> to nothing.

<p>
<a href="" onClick="sound.on(); sound.play(4); return false;">Sound On</a>
<a href="" onClick="sound.off(); return false;">Sound Off</a>

Comments:

You must also have soundctrl.class (1473 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/soundName.au">\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.