Previous | Next | Trail Map | Sound | Java Sound

Playing Sounds

Java Development Kittm 1.2 enables you to play many different types of audio files from both applets and applications.

Playing Sounds from an Applet

The mechanism for playing sounds from an applet is unchanged in JDK 1.2. To play a sound file, you can load the clip using Applet.getAudioClip and control playback through the AudioClip play, loop, and stop methods.

For example, to play a WAV file from an applet, you could:

  1. Call Applet.getAudio clip and pass in the URL where the .wav file is located.
  2. Call play or loop on the AudioClip.

The audio data is loaded when the AudioClip is constructed--it is not loaded asynchronously.

You can also use Applet.play to play any of the supported types of audio files. However, when you use Applet.play, the audio data is not preloaded. The first time the user initiates playback of a particular sound, your applet's drawing and event handling will freeze while the audio data is loaded.

Example: SoundApplet

This applet plays several different types of audio clips: an AU file, an AIFF file, a WAV file, and a MIDI file.


Note: The above applet requires JDK 1.2. If you are using a browser that does not support 1.2, you won't be able to run the applet. Instead, you need to view this page in a 1.2 browser such as the JDK Applet Viewer (appletviewer). For more information about running applets, refer to About Our Examples.
Because most browsers still don't support JDK 1.2, here's a picture of what the applet looks like:

Note:
You can find the complete code for this program in SoundApplet.java. You will also need these other files: And these sound files (shift-click each link to download the file):

Regardless of the type of sound file used, the code for loading and playing the file is the same. This example provides a framework for loading and playing multiple audio clips and loads the audio clips asynchronously, but the code for loading and playing the clips essentially boils down to:

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

This applet stops playing a looping sound when the user leaves the page and resumes playback when the user comes back. This is done through the applet's start and stop methods:

public void stop() {
    onceClip.stop();        //Cut short the one-time sound.
    if (looping) {
        loopClip.stop();    //Stop the sound loop.
    }
}    

public void start() {
    if (looping) {
        loopClip.loop();    //Restart the sound loop.
    }
}

To reduce the amount of time that the user has to wait before interacting with the applet, the sounds are preloaded in a background thread instead of in the applet's init method. If the user initiates playback before a sound has finished loading, the applet can respond appropriately. The actual loading of the sounds is done in the SoundLoader run method:

public void run() {
    AudioClip audioClip = applet.getAudioClip(baseURL, relativeURL);
    soundList.putClip(audioClip, relativeURL);
}

Playing Sounds from an Application

In JDK 1.2, applications as well as applets can play sounds. A new static method has been added to java.applet.Applet to enable applications to create AudioClips from a URL:
public static final AudioClip newAudioClip(URL r)
To play a sound from an applet, you call Applet.newAudioClip to load the sound and then use the AudioClip play, loop, and stop methods to control playback. For example, to play a WAV file from an application, you could:
  1. Call Applet.newAudioClip and pass in the URL where the .wav file is located.
  2. Call play or loop on the AudioClip.

Example: SoundApplication

The sound player in the previous example can easily be implemented as an application. The main difference is that Applet.newAudioClip is called to load the sounds.
AudioClip onceClip, loopClip;
onceClip = applet.newAudioClip(getCodeBase(), "file1.au");
loopClip = applet.newAudioClip(getCodeBase(), "file2.rmf");

Note: You can find the complete code for this program in SoundApplication.java. You will also need these other source files: And the sound files listed previously.


Previous | Next | Trail Map | Sound | Java Sound