368
|
Chapter 10, Audio
#72 Play a Sound with Java Media Framework
HACK
The downside to JavaSound is the small number of supported formats. Java-
Sound, at least out of the box, isn’t well suited to handle having random
sound formats thrown at it. That’s not a problem if you’re supplying your
own sounds for your application—just stick with AIFF or WAV. But it does
make JavaSound less than useful in a media browser or some other applica-
tion that is going to have to deal with random formats encountered on the
Net or the user’s local storage. Of course, that’s part of the idea of the
spi
sub-package: Sun provides the framework, and third parties make their for-
mats available to Java by implementing
spi interfaces.
H A C K
#72
Play a Sound with Java Media Framework Hack #72
Use the Java Media Framework for better performance and support for more
audio formats.
Java Media Framework (JMF) is Sun’s attempt to bring a broadly focused
multimedia framework to Java, supporting audio, video, and other time-
based media types. The idea is to provide Java desktop applications with
these features across operating systems. Like JavaSound, it’s meant to be
extended so that Sun or third parties could add support for new file formats
or codecs (the compression/decompression encoding schemes used inside
those files).
JMF offers another way to provide sound from an application. The advan-
tages of doing so are that JMF may provide access to many more sound files
than JavaSound will alone, and that JMF is somewhat easier to code than
JavaSound, particularly for simple tasks. The disadvantages are that JMF
capabilities vary wildly by platform, and that the end user will have to install
JMF separately, which will be difficult or simply not allowed for some users.
Installing JMF
Download and install JMF from its home page at http://java.sun.com/
products/java-media/jmf/index.jsp and you should be ready to go—no reboot
required. The installers should have put everything into the correct path and
set up your environment. If you have trouble getting JMF programs to run,
or if you used the all-Java version that doesn’t have a special installer, you
can try adding the following environment variable:
JMFHOME="C:\Program Files\JMF2.1.1"
Next, add the two JMF Java libraries to your classpath:
CLASSPATH="$JMFHOME\lib\jmf.jar;$JMFHOME\lib\sound.jar;.;$CLASSPATH"
PATH="$JMFHOME\lib;$PATH"

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.