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 “http://www.javazoom.net/javalayer/sources.html”.

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 “JLayerTest.java”, containing the following text.

import javazoom.jl.player.advanced.*;

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

        soundToPlay.play();
    }
}

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()
    {
        try
        {
            String urlAsString = 
                "file:///" 
                + new java.io.File(".").getCanonicalPath() 
                + "/" 
                + this.filePath;

            this.player = new AdvancedPlayer
            (
                new java.net.URL(urlAsString).openStream(),
                javazoom.jl.player.FactoryRegistry.systemRegistry().createAudioDevice()
            );

            this.player.setPlayBackListener(this);

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

            this.playerThread.start();
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    // PlaybackListener members

    public void playbackStarted(PlaybackEvent playbackEvent)
    {
        System.out.println("playbackStarted()");
    }

    public void playbackFinished(PlaybackEvent playbackEvent)
    {
        System.out.println("playbackEnded()");
    }    

    // Runnable members

    public void run()
    {
        try
        {
            this.player.play();
        }
        catch (javazoom.jl.decoder.JavaLayerException ex)
        {
            ex.printStackTrace();
        }

    }
}

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 %programName%.java

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

pause

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.

About these ads
This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

2 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?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s