Lesson 11.5 Sound
Java Sounds
Java lets you load a sound file, just as you can load an image. You can play the sound once, or again and again. I'm sure you'll be glad to hear that working with Java sounds is much, much simpler than working with Java graphics.
Unfortunately, the reason that working with sounds is so simple is because Java's sound facilities are so meager. This has been improved in Java 2, but that doesn't help those of us who want to add sound effects to our applets.
The Java 1.0 and Java 1.1 sound support is embodied in the AudioClip class. AudioClips:
- Only work in applets, not applications.
- Don’t work with PC speaker, only with your sound card.
- Only work with files in the .au format. This is common in the Unix world, but less so for the rest of the world.
If you already have .wav sounds which you'd like to play in your Java applets, you'll have to use a translator program such as GoldWave, AWave, or Sound Gadget to turn them into .au files.
Playing Sounds
As with displaying images, playing sounds is a three-step process:
- Create an AudioClip object and set its value to null.
- Load the audio clip by using the getAudioClip() method.
- Play the audio clip.
Loading the Sound
This getAudioClip() method looks almost exactly like getImage(), but the resemblence is only superficial:
- The getAudioClip() method does not return immediately like getImage().
- You don't have to do anything special to monitor sound loading. There is nothing like the MediaTracker class for sounds.
- If the sound cannot be found, the getAudioClip() returns null.
As with getImage(), you pass two arguments to getAudioClip(): a URL and a String, which holds the name of the file. Here's an example that loads the file "harp.au" from the same directory as the applet, and the file "click.au" from the sounds subdirectory beneath the current applet directory:
AudioClip harpAC = null;
AudioClip clickAC = null;
harpAC = getAudioClip(getCodeBase(),
"harp.au");
clickAC = getAudioClip(getCodeBase(),
"sounds/click.au"); |
As with images, applets can only load sounds from the same host that served the applet. That means you'll normally use getCodeBase() or getDocumentBase() to construct the URL for the method.
Playing Sounds
Once you have an AudioClip object in hand, you can perform it as often as you like, using either the method play() or the method loop().
As the names suggest, the play() method performs the sound a single time, while the loop() method repeats it over and over and over. Fortunately for everyone, the AudioClip class also has a stop() method you can use to end a clip that's looping.
You will normally use the loop() method for background music and use the play() method to respond to particular "special" events. Common sense suggests that you go easy on the background music.
Other than these simple controls, Java provides no other built-in sound capabilities. You may be interested in the Java Media Frameworks, JMF, however, if you want to do more high-end processing.
The Audio1 Applet
Audio1.java is an applet that uses each of the features you've learned in this lesson.
It begins by creating four AudioClip objects like this:
AudioClip oohAC = null;
AudioClip clickAC = null;
AudioClip harpAC = null;
AudioClip spaceAC = null; |
Inside the applet's init() method, three of the AudioClip objects are initialized using getAudioClip(). The fourth, spaceAC, which represents a larger file, is loaded later at user request.
Here's the code that retrieves the audio clips:
oohAC =
getAudioClip(getCodeBase(), "ooh.au");
clickAC =
getAudioClip(getCodeBase(),"click.au");
harpAC =
getAudioClip(getCodeBase(), "harp.au"); |
The applet creates a Button for each of the AudioClip objects, and a Button for playing, looping, and stoping an audio clip. The applet also contains a "current" AudioClip variable named ac, used to play the selected clip.
When the user clicks one of the AudioClip buttons, the current AudioClip variable, ac, is assigned to the actual AudioClip object the user selects. If the user clicks on the "spacemusic" button for the first time, then the AudioClip is retrieved at that time.
Here's the code for loading the "spacemusic" audio when selected:
...
else if (e.target == spaceBtn)
{
if (spaceAC == null)
{
showStatus("Loading spacemusic.au");
spaceAC = getAudioClip(getCodeBase(),
"spacemusic.au");
}
if (spaceAC != null)
showStatus("spacemusic.au is loaded");
ac = spaceAC;
}
... |
If the user presses one of the "action" buttons, such as "Play" or "Loop", then the appropriate action is carried out, after first checking to make sure the AudioClip variable, ac, is not null.
Here's the code that carries this out:
...
else if (ac != null)
{
if (e.target == playBtn)
ac.play();
else if (e.target == loopBtn)
ac.loop();
else if (e.target == stopBtn)
ac.stop();
}
... |
Here's the actual applet. Go ahead, try it out.
Something to Talk About
Can you modify FlyingTiger1.java from the previous lesson to play a sound whenever the tiger hits the edge? You may want to use the ooh.au file used by Audio1.java.
Please continue to the next section of this lesson.
|