Add MP3 Support to JMF #74
Chapter 10, Audio
|
377
HACK
a ZIP for other platforms. In both cases, there is an mp3plugin.jar file that
the install docs say you need to place in the ext/lib directory of any JRE you
want to provide the plug-in to. With the JAR in your classpath, you install
the plug-in with the following command:
java com.sun.media.codec.audio.mp3.JavaDecoder
On Mac OS X, the proper way to add JAR files to the class-
path is to put them in /Library/Java/Extensions, instead of
using the actual ext/lib directory, which is hard to find and
will be wiped out by system installers and updates anyways.
Simplicity Is Nice
Example 10-6 is the code for pretty much the simplest MP3 playing pro-
gram you can create. You will need to
import javax.media.* in addition to
the usual
java.io classes.
Those four lines in
main( ) do it all. The MediaLocator takes care of loading
and buffering the file as long as you can give it a URL. The player controls
the actual music output. It has the basic functions you would expect, like
start( ) and stop( ). If you compile and run this program with a test MP3
file, audio should start playing immediately.
Distribute Your Program
If you need to distribute your program, it’s best to include platform install-
ers and instruct users to install them first.
Licensing restrictions from Sun may apply here!
Example 10-6. A very basic MP3 player
import javax.media.*;
public class MP3Player {
public static void main(String[] args) throws Exception {
File file = new File("test.mp3");
MediaLocator mrl = new MediaLocator(file.toURL( ));
Player player = Manager.createPlayer(mrl);
player.start( );
}
}

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.