Previous | Next | Trail Map | Writing Applets | Creating an Applet User Interface


Playing Sounds

In the Java Applet package (java.applet), the Applet(in the API reference documentation) class and AudioClip(in the API reference documentation) interface provide basic support for playing sounds. Currently, the Java API supports only one sound format: 8 bit, µlaw, 8000 Hz, one-channel, Sun ".au" files. You can create these on a Sun workstation using the audiotool application. You can convert files from other sound formats using an audio format conversion program.

Sound-Related Methods

Below are the sound-related Applet methods. The two-argument form of each method takes a base URL, which is usually returned by either getDocumentBase or getCodeBase, and the location of the sound file relative to the base URL. You should use the code base for sounds that are integral to the applet. The document base is used for sounds specified by the applet user, such as through applet parameters.
getAudioClip(URL), getAudioClip(URL, String)(in the API reference documentation)
Return an object that implements the AudioClip interface.
play(URL), play(URL, String)(in the API reference documentation)
Play the AudioClip corresponding to the specified URL.

The AudioClip interface defines the following methods:

loop(in the API reference documentation)
Starts playing the clip repeatedly.
play(in the API reference documentation)
Plays the clip once.
stop(in the API reference documentation)
Stops the clip. Works with both looping and one-time sounds.

An Example

Here is an applet called SoundExample that illustrates a few things about sound. Note that, for instructional purposes, the applet adds up to 10 seconds to the load time for each sound. If the sounds were larger or the user's connection slower than ours, these delays might be realistic.


You can't run applets. Here's what you'd see if you could.


The SoundExample applet provides an architecture for loading and playing multiple sounds in an applet. For this reason, it is more complex than necessary. Essentially, the sound loading and playing code boils down to this:

AudioClip onceClip, loopClip;
onceClip = applet.getAudioClip(getCodeBase(), "bark.au");
loopClip = applet.getAudioClip(getCodeBase(), "train.au");
onceClip.play();     //Play it once.
loopClip.loop();     //Start the sound loop.
loopClip.stop();     //Stop the sound loop.

Since there's nothing more annoying than an applet that continues to make noise after you've left its page, the SoundExample applet stops playing the continuously looping sound when the user leaves the page, and resumes playing it when the user comes back. It does this by implementing its stop and start methods as follows:

public void stop() {
    //If one-time sound were long, we'd stop it here, too.  
    //looping is a boolean instance variable that's initially
    //false. It's set to true when the "Start sound loop" button
    //is clicked and to false when the "Stop sound loop" or "Reload
    //sounds" button is clicked.
    if (looping) {
        loopClip.stop();    //Stop the sound loop.
    }
}
 
public void start() {
    if (looping) {
        loopClip.loop();    //Restart the sound loop.
    }
}    

The SoundExample applet features three classes:

Preloading the sounds in a background thread (with SoundLoader) improves the perceived performance by reducing the amount of time the user has to wait to be able to interact with the applet. It does this by reducing the amount of time spent in the init method. If you simply called getAudioClip in the applet's init method, it could take quite a while before getAudioClip returned, meaning that the applet couldn't perform the other statements in its init method, and that the applet's start wouldn't get called. (For this SoundExample applet, a delay in calling the start method doesn't matter.)

Another advantage of loading the sounds in a background thread is that it enables the applet to respond appropriately (and immediately) to user input that would normally cause a sound to play, even if that sound hasn't been loaded yet. If you simply use the Applet play methods, for example, then the first time the user does something to make the applet play a particular sound, the applet's drawing and event handling are frozen while the sound is loaded. Instead, this applet detects that the sound hasn't been loaded yet and responds appropriately.

This example is discussed in more detail in Threads in Applets: Examples(in the Writing Applets trail).


Previous | Next | Trail Map | Writing Applets | Creating an Applet User Interface