Playing an MP3 from Java Using JLayer

JLayer is a Java library that allows MP3 files to be played from Java programs. The “traditional” way to play MP3 files from Java is the Java Media Framework, which is a huge, ponderous wad of programation that typically takes hours or days of heartrending labor to install, configure, and curse. JLayer, on the other hand, is available as a JAR file, and for the most part just works, even from an applet. (It doesn’t allow you to pause an MP3, which sucks, but I’ve prepared a hacktastic workaround in another post.)

1. If you have not already done so, download and install the Java SDK. Details are given in a previous tutorial. Make a note of the directory containing the file “javac.exe”.

2. In any convenient location, create a new directory named “JLayerTest”.

3. Download JLayer, by Javazoom. JLayer is a .jar library that allows MP3 files to be played from Java programs. As of this writing, JLayer is available as a ZIP archive at the URL “”.

4. Extract the files in a the JLayer archive to a directory, then open that directory and locate the JAR file containing the JLayer library. As of this writing, the correct file is named “jl1.0.1.jar”.

5. Copy the JLayer JAR file to the JLayerTest directory, and rename the file to “JLayer.jar”, for convenience.

6. Copy a test MP3 file to the JLayerTest directory. Rename the file to “Test.mp3”. (I had intended to include a sample file here, but unfortunately my WordPress account doesn’t allow me to upload MP3 files.)

7. Still in the JLayerTest directory, create a new text file named “”, containing the following text.

import javazoom.jl.player.advanced.*;

public class JLayerTest
    public static void main(String[] args)
        SoundJLayer soundToPlay = new SoundJLayer("Test.mp3");;

class SoundJLayer extends PlaybackListener implements Runnable
    private String filePath;
    private AdvancedPlayer player;
    private Thread playerThread;    

    public SoundJLayer(String filePath)
        this.filePath = filePath;

    public void play()
            String urlAsString = 
                + new".").getCanonicalPath() 
                + "/" 
                + this.filePath;

            this.player = new AdvancedPlayer


            this.playerThread = new Thread(this, "AudioPlayerThread");

        catch (Exception ex)

    // PlaybackListener members

    public void playbackStarted(PlaybackEvent playbackEvent)

    public void playbackFinished(PlaybackEvent playbackEvent)

    // Runnable members

    public void run()
        catch (javazoom.jl.decoder.JavaLayerException ex)


8. Still in the JLayerText directory, create a new text file named “JavaPathAndProgramNameSet.bat” containing the following text, and substitute the name of the directory containing javac.exe in the indicated place.

set javaPath="[path of directory containing javac.exe]"
for %%* in (.) do (set programName=%%~n*)

9. Still in the JLayerTest directory, create a new text file named “ProgramBuildAndRun-WithJLayerJar.bat”, containing the following text.

call JavaPathAndProgramNameSet.bat

%javaPath%\javac.exe -classpath .;JLayer.jar

%javaPath%\java.exe -classpath .;JLayer.jar %programName%


10. Double-click the icon for ProgramBuildAndRun-WithJLayer.jar to run it. A console window will appear, and the MP3 named Test.mp3 will play.


4 Responses to Playing an MP3 from Java Using JLayer

  1. Sushil Kumar says:

    This example worked like a charm for me.
    Good work.

  2. Mariusz says:

    How to stop playing?

  3. Aniri says:

    It doesn’t work for me…(win 8.1, 64bit, jdk 8) :(…

  4. Onur says:

    there is no stop! only u can play the file

Leave a Reply


You are commenting using your account. Log Out /  Change )

Google+ photo


Twitter picture


Facebook photo



Connecting to %s