Download the JDK
Search the Tutorials
Hide the TOC
Trail: Deployment
Lesson: Applets
Section: Taking Advantage of the Applet API
Applets
Getting Started with Applets
Importing Classes and Packages for Applets
Defining an Applet Subclass
The Life Cycle of an Applet
Methods for Milestones
Methods for Drawing and Event Handling
Methods for Adding UI Components
What Applets Can and Can't Do
Taking Advantage of the Applet API
Finding and Loading Data Files
Displaying Short Status Strings
Displaying Documents in the Browser
Sending Messages to Other Applets
Playing Sounds
Defining and Using Applet Parameters
Deciding Which Parameters to Support
Writing the Code to Support Parameters
Giving Information about Parameters
Using the APPLET Tag
Practical Considerations When Writing Applets
Security Restrictions
Creating a User Interface
Creating a GUI
Displaying Diagnostics to the Standard Output and Error Streams
Getting System Properties
Threads in Applets
Threads in Applets: Examples
Working with a Server-Side Application
A Simple Network Client Applet
Using a Server to Work Around Security Restrictions
Finishing an Applet
Before You Ship that Applet
The Perfectly Finished Applet
Deploying Applets
General Deployment Considerations
Using the applet Tag
Using the object Tag
Using the embed Tag
Deploying Applets in a Mixed-Browser Environment
Solving Common Applet Problems
Questions and Exercises
Home Page > Deployment > Applets
Playing Sounds

> The `JApplet` class in the Java Swing package (`javax.swing`), and the `AudioClip` interface in the Java Applet package (`java.applet`) 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.

> `getAudioClip(URL)`, `getAudioClip(URL, String)`
>   Return an object that implements the `AudioClip` interface.
> `play(URL)`, `play(URL, String)`
>   Play the `AudioClip` corresponding to the specified URL.

> The `AudioClip` interface defines the following methods:

> `loop`
>   Starts playing the clip repeatedly.
> `play`
>   Plays the clip once.
> `stop`
>   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.

> > Note: If you don't see the applet running above, you need to install Java Plug-in, which happens automatically when you install the Java(TM) SE JRE or JDK. This applet requires JDK 1.4 or later. You can find more information on the Java Plug-in home page.

> 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:

> - A `JApplet` subclass, `SoundExample`, that controls the applet's execution.
> - A `Hashtable` subclass, `SoundList`, that holds `AudioClip`s. This is overkill for this applet, but if you were to write an applet that used lots of sound files, a class like this would be useful.
> - A `Thread` subclass, `SoundLoader`, each instance of which loads an `AudioClip` in the background. During the applet's initialization, the applet preloads each sound by creating a `SoundLoader` for it.

> 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` method, 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.

Previous page: Sending Messages to Other Applets
Next page: Defining and Using Applet Parameters