364
|
Chapter 10, Audio
#71 Play a Sound with JavaSound
HACK
Realistically, you actually need to host the applet (HTML and code) on a
web server and access it via a URL. If you have access to a web server, put
index.html and AppletAudio.class in their own web directory, along with
some known-to-work audio files, then access it remotely with a browser.
Type in a URL to one of those audio files by hand and try to load it. This
time, it should work (finally).
But what formats are supported? Back in Java 1.0 and 1.1,
AudioClips had to
be encoded in mono at 8,000 Hz with µ-law encoding…which was pretty
awful. In Java 1.4.2, a few simple container formats are supported. AIFF and
WAV both work, but Mac OS X doesn’t support the .au format, which used
to be the only option (Windows can play .au, by the way). Also,
AudioClip
can’t play WAVs and AIFFs whose contents are compressed in any way.
And neither Mac nor Windows supports MP3 as
AudioClips.
You can also load audio from sites other than the one hosting your applet,
which seems contrary to the stringent applet security seen earlier.
So, using
java.awt.applet’s AudioClip means your application has to be an
applet, it can only run a handful of audio formats, and your media can’t be
local unless you go through the hassle of overriding the
SecurityManager for
the applet.
As you might imagine, early Java developers demanded a replacement.
H A C K
#71
Play a Sound with JavaSound Hack #71
Get a small clip to play from memory with a lot less hassle.
The JavaSound API was developed to answer complaints about the inade-
quacies of the
AudioClip class in the applet package…not the least of which
was the fact that it couldn’t be used in applications. JavaSound consists of
two packages—
javax.sound.sampled and javax.sound.midi—plus two ser-
vice provider interface (
spi) sub-packages for adding support for new
devices, formats, converters, etc.
JavaSound was introduced as an extension to Java 1.2 (I know, I know,
“Java 2 Standard Edition, version 1.2”), and it became part of Core Java in
1.3. In other words, you’re pretty safe assuming that it’s present on your
user’s machine. That’s one big point in its favor.
Putting JavaSound to Work
To show off JavaSound, the code in Example 10-3 exhibits a short applica-
tion that allows the user to pick a file from the local filesystem and play it. A
Play a Sound with JavaSound #71
Chapter 10, Audio
|
365
HACK
dialog shows the selected filename; OK it when the audio completes to exit
the program.
Example 10-3. Playing audio with JavaSound
public class CoreJavaSound extends Object
implements LineListener {
File soundFile;
JDialog playingDialog;
Clip clip;
public static void main (String[] args) {
JFileChooser chooser = new JFileChooser( );
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile( );
try {
CoreJavaSound s = new CoreJavaSound (f);
} catch (Exception e) {
e.printStackTrace( );
}
}
public CoreJavaSound (File f)
throws LineUnavailableException, IOException,
UnsupportedAudioFileException {
soundFile = f;
// prepare a dialog to display while playing
JOptionPane pane = new JOptionPane ("Playing " + f.getName( ),
JOptionPane.PLAIN_MESSAGE);
playingDialog = pane.createDialog (null, "Application Sound");
playingDialog.pack( );
// get and play sound
Line.Info linfo = new Line.Info (Clip.class);
Line line = AudioSystem.getLine (linfo);
clip = (Clip) line;
clip.addLineListener (this);
AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
clip.open (ais);
clip.start( );
}
// LineListener
public void update (LineEvent le) {
LineEvent.Type type = le.getType( );
if (type == LineEvent.Type.OPEN) {
System.out.println ("OPEN");
} else if (type == LineEvent.Type.CLOSE) {
System.out.println ("CLOSE");
System.exit (0);

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.