|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The
JAppletclass in the Java Swing package (
javax.swing), and theAudioClipinterface 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 theaudiotoolapplication. You can convert files from other sound formats using an audio format conversion program.
Below are the sound-related
Appletmethods. The two-argument form of each method takes a base URL, which is usually returned by eithergetDocumentBaseorgetCodeBase, and the location of the sound file relative to the base URL.
getAudioClip(URL),getAudioClip(URL, String)![]()
- Return an object that implements the
AudioClipinterface.play(URL),play(URL, String)![]()
- Play the
AudioClipcorresponding to the specified URL.The
AudioClipinterface 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 J2SE JRE or JDK. This applet requires JDK 5.0 or later. You can find more information in 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
stopandstartmethods 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
JAppletsubclass,SoundExample, that controls the applet's execution.
- A
Hashtablesubclass,SoundList, that holds
AudioClips. 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
Threadsubclass,SoundLoader, each instance of which loads an
AudioClipin the background. During the applet's initialization, the applet preloads each sound by creating aSoundLoaderfor 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 theinitmethod. If you simply calledgetAudioClipin the applet'sinitmethod, it could take quite a while beforegetAudioClipreturned, meaning that the applet couldn't perform the other statements in itsinitmethod, and that the applet'sstartwouldn't get called. (For this SoundExample applet, a delay in calling thestartmethod 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 playmethods, 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
.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.