soundSingle
Class SoundSingle

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

public class SoundSingle
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. With this class you may also load() a new sound. However only one sound is availiable at a time. (Why not more? To preserve the end users memory, that's why! And because it normally gets cached, no performance hurt anyway. I thought about this for a while.) It was designed for those that have many sounds on a site, and can't possibly specify them all at once.

Works with:
JDK1.0
See Also:
Sound SoundExt SoundCtrl

Constructor Summary
SoundSingle()
 
 
Method Summary
 void load(String fileName)
           Loads fileName, overwriting the previously held sound.
 void on()
           Enables sound playing.
 void off()
           Disables sound playing until on is called.
 boolean isOn()
           Reports if sounds can be played.
 void play()
           Called to play the sound.
 void loop()
           Called to begin looping the sound.
 void stop(int index)
           Called to stop the sound.
 void init()
          Called by the browser or applet viewer to load the sounds. The sound is specified by the <PARAM>.

Constructor Detail

SoundSingle

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

load

public void load(String fileName)
Loads fileName, overwriting the previously held sound. With this class you may also load() a new sound. However only one sound is availiable at a time. (Why not more? To preserve the end users memory, that's why! And because it normally gets cached, no performance hurt anyway. I thought about this for a while.)
Parameters:
fileName - the relative URL of the sound file.

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 the 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()
Plays the 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().

loop

public void loop()
Begins to loop the 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 is currently off().


stop

public void stop()
Stops the 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.

init

public void init()
Begins loading the sound.

It looks for the parameter "sound0". 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

View the source code for the websounds page.

Comments:

You must also have soundsingle.class (1111 bytes), as well as the sounds, in the current directory (or somewhere you can get at them).