
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 ...