The
JApplet
class in the Java Swing package (javax.swing
), and theAudioClip
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 theaudiotool
application. You can convert files from other sound formats using an audio format conversion program.
Below are the sound-related
Applet
methods. The two-argument form of each method takes a base URL, which is usually returned by eithergetDocumentBase
orgetCodeBase
, 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:
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
andstart
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 holdsAudioClip
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 anAudioClip
in the background. During the applet's initialization, the applet preloads each sound by creating aSoundLoader
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 theinit
method. If you simply calledgetAudioClip
in the applet'sinit
method, it could take quite a while beforegetAudioClip
returned, meaning that the applet couldn't perform the other statements in itsinit
method, and that the applet'sstart
wouldn't get called. (For this SoundExample applet, a delay in calling thestart
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.