How to Play a MP3 File in Java – Simple and Beautiful

This is a very short tutorial, focusing on setting up your java environment for playing a simple MP3 file. I’m using the Sun Java Media Framework along with the MP3 Plugin. So all we have to do is, get the right files, set up the project and add the right code.

  1. Goto http://java.sun.com/javase/technologies/desktop/media/jmf/2.1.1/download.html
  2. Download the Linux Cross Plattfom Version, because it is packed along with a jar file, so you don’t have to install stuff.
  3. Unzip the the file and copy the “jmf.jar”-file, which is located in the lib folder, to your project’s lib folder.
    You won’t need the other files.
  4. Next we need to download the MP3 PlugIn that allows us to play MP3 files. The standard Java Media Framework only supports WAV and AIFF files.
    So goto http://java.sun.com/javase/technologies/desktop/media/jmf/mp3/download.html click download and select “other”.
  5. Download and unpack the file.
  6. Copy the file “mp3plugin.jar” from the lib folder to your project’s lib folder.
  7. Make sure to add the jars to the compiler command.
  8. add the code to a file
    package test;
    
    import java.io.File;
    
    import javax.media.Format;
    import javax.media.Manager;
    import javax.media.MediaLocator;
    import javax.media.Player;
    import javax.media.PlugInManager;
    import javax.media.format.AudioFormat;
    
    public class AudioTest {
    	public static void main(String[] args) {
    		Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
    		Format input2 = new AudioFormat(AudioFormat.MPEG);
    		Format output = new AudioFormat(AudioFormat.LINEAR);
    		PlugInManager.addPlugIn(
    			"com.sun.media.codec.audio.mp3.JavaDecoder",
    			new Format[]{input1, input2},
    			new Format[]{output},
    			PlugInManager.CODEC
    		);
    		try{
    			Player player = Manager.createPlayer(new MediaLocator(new File("data/audioFiles/dusche.mp3").toURI().toURL()));
    			player.start();
    		}
    		catch(Exception ex){
    			ex.printStackTrace();
    		}
    	}
    }
    
  9. Fix the path to your mp3 File.
  10. done

This piece of code is quite important, because it registers the PlugIn.

		Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
		Format input2 = new AudioFormat(AudioFormat.MPEG);
		Format output = new AudioFormat(AudioFormat.LINEAR);
		PlugInManager.addPlugIn(
			"com.sun.media.codec.audio.mp3.JavaDecoder",
			new Format[]{input1, input2},
			new Format[]{output},
			PlugInManager.CODEC
		);

Questions or suggestions?

Is there an easier way?

Links:

13 thoughts on “How to Play a MP3 File in Java – Simple and Beautiful”

  1. This article will be very helpful. I’m looking for articles that would help me learn opening other kind of files. Thanks so much!

  2. you could easily do this on the player instance. there is the gaincontrol which has the specific methods.

    your code may look like this:

    player.getGainControl().setLevel(v);

    where v is a float between 0 and 1.

  3. OK, I used your code and I don’t get an error, but volume doesn’t change. I tried setDB(float v) method, but result is the same.

    Here is my play function:

    public void Play()
    {
    try {
    player = Manager.createRealizedPlayer(new MediaLocator (new File (path).toURI().toURL() ) );
    } catch (NoPlayerException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (CannotRealizeException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    float v = (float) 0.1;
    player.getGainControl().setLevel(v);
    player.start();
    }

  4. when i used the above code and run the program…..its not displaying or playing anything…..plz give me some solution…:-(….

Leave a Reply