Add MP3 Support to JMF





Add MP3 Support to JMF

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 easier 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, download 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 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 classpath 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

Figure is the code for pretty much the simplest MP3 playing program you can create. You will need to import javax.media.* in addition to the usual java.io classes.

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();
	}
}

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 installers and instruct users to install them first.

Licensing restrictions from Sun may apply here!


Even better, if you have a custom install program, make it run the JMF and MP3 installers first. If you don't have an installer or don't know which platform the program will be installed on, use the cross-platform versions (also available from the URLs mentioned in this hack). They are slower but are completely written in Java so they can run on anything.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows