Playing Audio in an Applet
Applets can also play short sound clips, using the same "load once in init(), use it later"
pattern you just saw for images. The type involved this time is java.applet.AudioClip, and
it's about as simple as AWT-era APIs get.
Loading a Clip: getAudioClip()
Just like getImage(), getAudioClip() takes a base URL and a filename, and is
best called once inside init():
AudioClip clip = getAudioClip(getDocumentBase(), "click.wav");
The base-URL choice works exactly as covered in Displaying Images:
getDocumentBase() for a file next to the HTML page, getCodeBase() for one
bundled next to the compiled classes.
.au format above all others, since it
was supported from the very earliest JDKs. Later JDKs (before the whole API was removed) also supported
more common formats like .wav and .aiff. If you're following along with an old
JDK, .wav is the safest, most widely-supported choice for new examples.
The AudioClip Interface
AudioClip is refreshingly small - just three methods:
| Method | Effect |
|---|---|
play() | Plays the clip once, from the start |
loop() | Plays the clip repeatedly, looping until stop() is called |
stop() | Stops playback, whether it was started with play() or loop() |
There's also a static convenience method, Applet.newAudioClip(URL url), which builds an
AudioClip straight from a URL without needing an actual Applet instance to call
it on - handy for utility code that isn't itself part of the applet.
Extending PaintApplet: A Click Sound
PaintApplet currently only listens for mouse movement, via the
MouseMotionAdapter registered with addMouseMotionListener() back in
Handling Mouse Events. To play a sound the moment the user
presses the mouse button - starting a new stroke - we need a second, separate registration: a
MouseListener (via its MouseAdapter convenience class), added with
addMouseListener(). These are two different interfaces even though both are "mouse" events -
one reports button presses/releases/clicks, the other reports movement/dragging.
// New field
private AudioClip clickSound;
public void init() {
// ...existing title/bgcolor/background-image handling stays here...
// New: load the click sound
clickSound = getAudioClip(getDocumentBase(), "click.wav");
// ...existing addMouseMotionListener(...) for dragging stays here...
// New: a separate listener for the mouse press itself
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if (clickSound != null) {
clickSound.play();
}
}
});
// ...existing addKeyListener(...) for color-switching stays here...
}
After this change, init() registers two separate mouse-related listeners on the same applet:
addMouseMotionListener() keeps handling mouseDragged() to build up the list of
drawn points, and the new addMouseListener() handles mousePressed() to fire a
sound effect. AWT is happy to deliver both kinds of event to the same component independently - you're
not replacing anything, just adding a second, unrelated listener alongside the first.