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
Hi,
ReplyDeleteI 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>
The error says class not found. You got to compile the java file first.
ReplyDeleteDo javac amp3.java and then
java amp3
Cheers!
Now I blog here at http://goo.gl/dcaKE
ReplyDeleteCheers!
Can we control the volume?
ReplyDelete