The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Sound

## Playing Sounds

> With JDK 1.2 you can play many different types of audio files from both applets and applications. This lesson contains three sections that show you how.

### 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 by 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

> The applet shown below plays several types of audio clips: an AU file, an AIFF file, a WAV file, and a MIDI file. (The AIFF and WAV files used in the examples for this trail were provided by Headspace, Inc.)

> This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.

> You can find the complete code for this program in `SoundApplet.java`. The applet also requires two more source files, `AppletSoundList.java` and `AppletSoundLoader.java`, and the following sound files:

> - `bottle-open.wav`
> - `flute+hrn+mrmba.aif`
> - `jungle.rmf`
> - `spacemusic.au`
> - `trippygaia1.mid`

> > Note: If you're behind a firewall, the SoundApplet sample might not play the sounds if you run it by clicking on the picture above. Some versions of Java Plugin cannot access the sound files from a JAR file. To run the applet, you can download all of the necessary files to your machine and run it locally without using a JAR 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 = soundList.getClip(chosenFile);
> >     loopClip = soundList.getClip(chosenFile);
> >     AudioClip audioClip = Applet.getAudioClip(baseURL, relativeURL);
> >     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 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 = soundList.getClip(chosenFile);
> >     loopClip = soundList.getClip(chosenFile);
> >     AudioClip audioClip = Applet.newAudioClip(completeURL);

> You can find the complete code for this application in `SoundApplication.java`. To run this application, you will also need the files `SoundList.java` and `SoundLoader.java`, and the following sound files:
> - `bottle-open.wav`
> - `flute+hrn+mrmba.aif`
> - `jungle.rmf`
> - `spacemusic.au`
> - `trippygaia1.mid`

### Common Problems (and Their Solutions)

> Here are solutions to two of the most common problems you might encounter when playing audio files.
>   Problem: 8 kHz audio files do not sound as good as they did before I switched to JDK 1.2.
>   Solution: The Java Sound engine up-samples 8 kHz audio data to 22 kHz, which can result in added noise during playback. If you find that the audio quality is not acceptable, start with a higher-quality audio clip to avoid the up-sampling.

>   Problem: Some of my audio files won't play.
>   Solution: You cannot play WAV, AU, AIFF, or AU files compressed using ADPCM or other compression schemes. The Java Sound engine supports only linear PCM audio files.

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.