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.
- Goto http://java.sun.com/javase/technologies/desktop/media/jmf/2.1.1/download.html
- Download the Linux Cross Plattfom Version, because it is packed along with a jar file, so you don’t have to install stuff.
- 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. - 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”. - Download and unpack the file.
- Copy the file “mp3plugin.jar” from the lib folder to your project’s lib folder.
- Make sure to add the jars to the compiler command.
- 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(); } } } - Fix the path to your mp3 File.
- 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:
Thanks for share the simple solution for play mp3 with JMF.
Helped me!
that’s good to hear!
Nope, if install now download mp3plugin, it registered auto:-)
Just use the player to listen:)
i’ll give it a try!
This article will be very helpful. I’m looking for articles that would help me learn opening other kind of files. Thanks so much!
OMG it works! Thanks a lot :)
I have a question. How can I change the volume in this example? Thanks in advance.
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.
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();
}
have you tried starting before setting the level?
Please can u help me for the video run like that simple and beautiful
when i used the above code and run the program…..its not displaying or playing anything…..plz give me some solution…:-(….
Great tutorial …………Get it after 4 hour of searching