Tuesday, September 14

Playing MP3 in Java Programming Language Using JLayer

Yeah..! and finally its done.. I was havin problem wid. Now I got a good library (JLayer) and I am able to play MP3 files. I had a confusion whether to use JMF(Java Media Framework), GStreamer, JLayer or any other alternatives... And now I am attached with JLayer... probably I think :)

So I am here to show you a simple method of playing MP3 file in Java Programming Language using third party library JLayer (licensed under LGPL)

So if you are to make a simple MP3 player then you first got to download this library JLayer
download from JLayer Homepage
download from Sourceforge

Now you need unzip the downloaded file and add jl1.0.1.jar file in your class path.

Here's the basic programming source code for playing mp3 file:

import java.io.BufferedInputStream;
import java.io.FileInputStream;

import javazoom.jl.player.Player;


public class MP3 {
    private String filename;
    private Player player;

    // constructor that takes the name of an MP3 file
    public MP3(String filename) {
        this.filename = filename;
    }

    // play the MP3 file
    public void play() {
        try {
            FileInputStream fis     = new FileInputStream(filename);
            BufferedInputStream bis = new BufferedInputStream(fis);
            player = new Player(bis);
            player.play();
        }
        catch (Exception e) {
            System.out.println("Problem playing file " + filename);
            System.out.println(e);
        }

        
    }

    
    public static void main(String[] args) {
        
        //plays 07.mp3 file located at C drive
        MP3 mp3 = new MP3("c:/07.mp3");
        mp3.play();
   
    }

}


If you have any sort of problem with above method, then please feel free to leave a comment...
Enjoy the music.. :D

4 comments:

  1. Hi,
    I got an error while executing the above code.Please help me i want to play the mp3 songs.The error is:

    C:\Program Files\Java\jdk1.6.0_10\bin>java amp3
    Exception in thread "main" java.lang.NoClassDefFoundError: amp3
    Caused by: java.lang.ClassNotFoundException: amp3
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    Could not find the main class: amp3. Program will exit.

    C:\Program Files\Java\jdk1.6.0_10\bin>

    ReplyDelete
  2. The error says class not found. You got to compile the java file first.
    Do javac amp3.java and then
    java amp3

    Cheers!

    ReplyDelete
  3. Now I blog here at http://goo.gl/dcaKE

    Cheers!

    ReplyDelete
  4. Can we control the volume?

    ReplyDelete