O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

376
|
Chapter 10, Audio
#74 Add MP3 Support to JMF
HACK
Running the Code
Like the JMF port [Hack #72], QTJSound looks and feels more or less the same as
the original CoreJavaSound
[Hack #71]
. The big difference is in the supported
sound file formats. QuickTime will open not just uncompressed WAVs and
AIFFs, but compressed data in those formats, along with MP3s, AACs,
3GPP mobile audio files, iTunes Music Store files, audio tracks of various
audio/video formats like QuickTime movies (.mov), MPEG-4, even audio
CD tracks (but only on the Mac).
That’s obviously the big win with QuickTime for Java: you get support for a
lot more formats. The price you pay is that your code only runs on two
operating systems and that it can be difficult to write. The ideal would be if
the obvious points of extensibility in JavaSound and JMF had been
exploited, so that more formats would be available when using those APIs.
With a notable exception
[Hack #74], that hasn’t happened yet.
By the way, to learn more about QuickTime for Java, check
out QuickTime for Java: A Developer’s Notebook (O’Reilly).
H A C K
#74
Add MP3 Support to JMF Hack #74
MP3s are everywhere, and by installing a plug-in you can use them with Java
Media Framework, too.
It used to be said that every program will continue to grow until it includes
an email reader. Today we could say the same for MP3 players. They are
everywhere, and any program that has plug-ins will eventually be given a
music player. Playing MP3s in Java used to be quite an ordeal, involving a
suite of toolkits and codecs from different sources. Fortunately, it’s a lot eas-
ier to play an MP3 file these days, and this hack shows how.
JMF came out in 1998, supporting playback of a number of audio and video
formats, but not MP3. Support for this popular format arrived with JMF 2.0
in 1999. Unfortunately, in 2002, Sun removed MP3 support from JMF
because of licensing problems. Finally, in November of 2004, Sun released a
fully licensed MP3 plug-in for public download on their web site. With this
plug-in, you can play any MP3 file with only four lines of code.
Add a Plug-In to JMF
First, install Java Media Framework [Hack #72]. To add MP3 support, down-
load the plug-in from http://java.sun.com/products/java-media/jmf/mp3/
download.html. The download page offers an .exe installer for Windows and

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required