Click here to Skip to main content
15,881,027 members
Articles / Programming Languages / Java
Tip/Trick

Simple MP3 player in Java

Rate me:
Please Sign up or sign in to vote.
4.14/5 (7 votes)
16 Nov 2011CPOL1 min read 170.6K   6   6
Make a music player using 3rd party libs in Java

Making a music player using JMF may seem a bit complex. There are a few Open Source libs that will help you out. I have made a collection of these and they are freely available here: Music Libraries

After downloading, you need to create a Java project in your favorite IDE and set these libs in your project classpath. (These libs are development from JavaZoom, JLayer MP3 API.) Now after all these are done, a simple code will play your song.


Java
BasicPlayer myMusicPlayer=new BasicPlayer();
BasicController playerController =(BasicController)myMusicPlayer;
String filePath="D:/SexyTracks/Just_dance.mp3";
File file=new File(filePath);
try
{ 
    playerController.open(file);
    playerController.play();

}catch(Exception ex){}

To pause or stop the song:


Java
try
{
   //choice can be any boolean,I just included it to show the pause & stop         
   if(choice)
     playerController.pause();
   else
     playerController.stop();

}catch(Exception ex){}

Or you might like to set the volume using:


Java
/*The setGain() method takes double within 0.0 to 1.0.
1 is for full sound(100%)*/
playerController.setGain(0.85);

Remember one thing. Whatever you do with the controller, don't forget to surround it with try/catch blocks. The API creates a separate thread for each song that gets played, so you don't need to worry about creating a new thread.


In case you need to know how much of the song has been played so far or when the song was opened, paused, or anything, you need a BasicPlayerListener. In the class where you are playing the song, make it implement BasicPlayerListener.


Java
class MyMusicPlayer implements BasicPlayerListener

And add these method stubs (you will need others also according to your purpose):


Java
/**
* Open callback, stream is ready to play.
*
* properties map includes audio format dependant features such as
* bitrate, duration, frequency, channels, number of frames, vbr flag, ...
*
* @param stream could be File, URL or InputStream
* @param properties audio stream properties.
*/
public void opened(Object stream, Map properties)
{
}
/** * Progress callback while playing.
*
* This method is called severals time per seconds while playing.
* properties map includes audio format features such as
* instant bitrate, microseconds position, current frame number, ...
*
* @param bytesread from encoded stream.
* @param microseconds elapsed (reseted after a seek !).
* @param pcmdata PCM samples.
* @param properties audio stream parameters.
*/
public void progress(int bytesread, long microseconds, byte[] pcmdata, Map properties)
{
    /*shows you how much seconds of the song has been played*/
    long time=microseconds/(1000*1000);//time in seconds
    System.out.println(time);
    // Pay attention to properties. It depends on underlying JavaSound SPI
    // MP3SPI provides mp3.equalizer.
}

In case you need a speed up, here's my simple NetBeans project[^] for you.


I think this will warm you up enough to get started with this strong music library from JavaZoom[^].:)


N.B.: The way described here will enable you to play MP3 files only. To be able to play other file types, include the full set of libs I mentioned before in your project classpath.


Don't forget to let me know your steps.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior) ManGoes Mobile,Inc.
Bangladesh Bangladesh
Employment:

ManGoes Mobile, Inc.
Software Developer-Android & iOS platform, present
Udvash Academic & Admission Care
Lecturer,Physics Department, present

Education:

Bangladesh University of Engineering & Technology
Computer Science & Engineering, present
Dhaka City College
Major-Science, 2006 - 2008

Interests:

Software designing,UX designing,Architecture designing,
Graphics designing.

Comments and Discussions

 
Questionis there a way... Pin
JonHonHH5-Nov-13 1:54
JonHonHH5-Nov-13 1:54 
GeneralExtremely sorry for your inconvenience.Thanks for the link.U... Pin
Coderguy19416-Nov-11 4:18
Coderguy19416-Nov-11 4:18 
GeneralSorry for the inconvenience Tisma. Updated the link. Please ... Pin
Coderguy19415-Nov-11 5:19
Coderguy19415-Nov-11 5:19 
GeneralRe: links from woofiles still dont work Pin
Ibsy15-Nov-11 22:53
professionalIbsy15-Nov-11 22:53 
GeneralRe: When I click on that link above I got a message on white pag... Pin
Miroslav Tisma15-Nov-11 23:49
professionalMiroslav Tisma15-Nov-11 23:49 
GeneralI can't download that library from woofiles.com. Pin
Miroslav Tisma15-Nov-11 4:34
professionalMiroslav Tisma15-Nov-11 4:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.