Hello friends, welcome to my other tutorial, and in this tutorial, you will learn about How to Play Mp3 File in Java. Most of us like to listen to music, and nothing could be more pleasing to us if we can make and customize our Music player. By going through this article, you will get to know how you can play an mp3 file in Java and also you will create a simple music player.
I am going to use Intellij IDEA IDE for the programming development, but you are free to use any IDE like NetBeans or Eclipse. In any case, if you want to download the source code of the program, then the link of the file is given at the end of the tutorial.
So let’s start our tutorial How to Play Mp3 File in Java and learn step by step.
How to Play Mp3 File in Java
Downloading jlayer Jar File
step 1
- To play any mp3 file in java, you need to download a jar file called jlayer and add it to your java project.
- To download the jlayer jar file, you can simply click on the link given below.
- After downloading the jlayer jar file, the next thing you have to do is to add it to your project.
Adding jlayer Jar File to Our Project
step 2
- Create a new Java project in your IDE and give it a name. You can use any IDE like NetBeans, Eclipse or IntelliJ IDEA to create your Java project.
- For NetBeans users,
- Right click on the Libraries folder then select Add JAR/Folder and then you can add the JAR file to your project.
- For Eclipse users,
- Right click on the Project then select Properties —>>>>Java Build Path and then you can choose Add External JARs button to add the JAR file.
- For IntelliJ IDEA users,
- Go to File––>>>>Project Structure—>>>>Libraries then click on the “+” sign and select Java and then you can add the JAR file.
- Now in the next step, you have to prepare a display for music player.
Preparing Display for Our Music Player
step 3
- Create a class(MusicPlayer in this example).
- Now create an object of the JFrame class and set some of its properties like its Title, Background color, its Layout, its Size and Location and its Default Close Operation.
- Now create an object of five JButtons(Select Mp3, Play, Pause, Resume and Stop) and one JLabel( songNameLabel).
- Now you have to set the size and location of JButtons and JLabel using the setBounds() method and then finally you have to add them to the JFrame using the add() method.
- Program is given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
//importing necessary packages import javax.swing.*; import java.awt.*; public class MusicPlayer { JFrame frame;//Creating object of JFrame JLabel songNameLabel=new JLabel();//Creating object of song name label //Creating object of JButtons JButton selectButton=new JButton("Select Mp3"); JButton playButton=new JButton("Play"); JButton pauseButton=new JButton("Pause"); JButton resumeButton=new JButton("Resume"); JButton stopButton=new JButton("Stop"); MusicPlayer(){ prepareGUI(); } public void prepareGUI(){ //Setting properties of JFrame frame=new JFrame(); frame.setTitle("Music Player"); frame.getContentPane().setLayout(null); frame.getContentPane().setBackground(Color.pink); frame.setSize(440,200); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Setting properties of select mp3 button selectButton.setBounds(160,10,100,30); frame.add(selectButton); //Setting properties of song name label songNameLabel.setBounds(100,50,300,30); frame.add(songNameLabel); //Setting properties of play button playButton.setBounds(30,110,100,30); frame.add(playButton); //Setting properties of pause button pauseButton.setBounds(120,110,100,30); frame.add(pauseButton); //Setting properties of resume button resumeButton.setBounds(210,110,100,30); frame.add(resumeButton); //Setting properties of stop button stopButton.setBounds(300,110,100,30); frame.add(stopButton); } } |
- Now create another class(MainClass in this example) in the same project and inside main() method create an object of MusicPlayer class that you have created earlier.
1 2 3 4 5 |
public class MainClass { public static void main(String[] args){ new MusicPlayer(); } } |
- Now Run your Program.
- As you can see that we have successfully created display for music player.
- Now the next thing you have to do is to add functionalities to your buttons.
Adding Functionalities to Buttons
step 4
- To add functionalities to your buttons, you must have to implement the ActionListener interface in your class. Then you have to override its method actionPerformed() in that class. If you don’t know how to add ActionListener to any button, then you can refer to my tutorial Java Button Click Event.
- Now we want that when we click on the Select Mp3 button, a dialog window should appear from which we can select the Mp3 file.
- To perform the task of choosing the file from the dialog window, we will use the JFileChooser class of Java swing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
//importing all necessary packages import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; //implementing ActionListener interface public class MusicPlayer implements ActionListener { JFrame frame; JLabel songNameLabel=new JLabel(); JButton selectButton=new JButton("Select Mp3"); JButton playButton=new JButton("Play"); JButton pauseButton=new JButton("Pause"); JButton resumeButton=new JButton("Resume"); JButton stopButton=new JButton("Stop"); JFileChooser fileChooser;//Creating object of JFileChooser File myFile=null;//Creating object of File class String filename; String filePath; MusicPlayer(){ prepareGUI(); addActionEvents(); } public void prepareGUI(){ frame=new JFrame(); frame.setTitle("Music Player"); frame.getContentPane().setLayout(null); frame.getContentPane().setBackground(Color.pink); frame.setSize(440,200); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); selectButton.setBounds(160,10,100,30); frame.add(selectButton); songNameLabel.setBounds(100,50,300,30); frame.add(songNameLabel); playButton.setBounds(30,110,100,30); frame.add(playButton); pauseButton.setBounds(120,110,100,30); frame.add(pauseButton); resumeButton.setBounds(210,110,100,30); frame.add(resumeButton); stopButton.setBounds(300,110,100,30); frame.add(stopButton); } public void addActionEvents(){ selectButton.addActionListener(this); } //Overriding actionPerformed() method @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==selectButton){ //Setting actions that should be performed when Select Mp3 button is clicked. fileChooser=new JFileChooser(); fileChooser.setCurrentDirectory(new File("C:\\Users\\hamee\\Downloads")); fileChooser.setDialogTitle("Select Mp3"); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setFileFilter(new FileNameExtensionFilter("Mp3 files","mp3")); if(fileChooser.showOpenDialog(selectButton)==JFileChooser.APPROVE_OPTION){ myFile=fileChooser.getSelectedFile(); filename=fileChooser.getSelectedFile().getName(); filePath=fileChooser.getSelectedFile().getPath(); } } } } |
- Now we will add functionalities to our all buttons so that when we click on the Play button the mp3 file that we have selected should be played, and when we click on the pause button, then it should be paused, when we click on the Resume button, it should be resumed from where it had been paused. After clicking on the Stop button, the music should be stopped.
- To achieve this task, you must have to break your program into several threads so that your GUI will not freeze while doing a particular activity.
- The final program of the tutorial is given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
//importing all necessary packages import javazoom.jl.decoder.JavaLayerException; import javazoom.jl.player.Player; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; //implementing ActionListener interface public class MusicPlayer implements ActionListener { JFrame frame; JLabel songNameLabel=new JLabel(); JButton selectButton=new JButton("Select Mp3"); JButton playButton=new JButton("Play"); JButton pauseButton=new JButton("Pause"); JButton resumeButton=new JButton("Resume"); JButton stopButton=new JButton("Stop"); JFileChooser fileChooser; FileInputStream fileInputStream; BufferedInputStream bufferedInputStream; File myFile=null; String filename; String filePath; long totalLength; long pause; Player player; Thread playThread; Thread resumeThread; MusicPlayer(){ prepareGUI(); addActionEvents(); playThread=new Thread(runnablePlay); resumeThread=new Thread(runnableResume); } public void prepareGUI(){ frame=new JFrame(); frame.setTitle("Music Player"); frame.getContentPane().setLayout(null); frame.getContentPane().setBackground(Color.pink); frame.setSize(440,200); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); selectButton.setBounds(160,10,100,30); frame.add(selectButton); songNameLabel.setBounds(100,50,300,30); frame.add(songNameLabel); playButton.setBounds(30,110,100,30); frame.add(playButton); pauseButton.setBounds(120,110,100,30); frame.add(pauseButton); resumeButton.setBounds(210,110,100,30); frame.add(resumeButton); stopButton.setBounds(300,110,100,30); frame.add(stopButton); } public void addActionEvents(){ //registering action listener to buttons selectButton.addActionListener(this); playButton.addActionListener(this); pauseButton.addActionListener(this); resumeButton.addActionListener(this); stopButton.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==selectButton){ //code for selecting our mp3 file from dialog window fileChooser=new JFileChooser(); fileChooser.setCurrentDirectory(new File("C:\\Users\\hamee\\Downloads")); fileChooser.setDialogTitle("Select Mp3"); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setFileFilter(new FileNameExtensionFilter("Mp3 files","mp3")); if(fileChooser.showOpenDialog(selectButton)==JFileChooser.APPROVE_OPTION){ myFile=fileChooser.getSelectedFile(); filename=fileChooser.getSelectedFile().getName(); filePath=fileChooser.getSelectedFile().getPath(); } } if(e.getSource()==playButton){ //starting play thread playThread.start(); songNameLabel.setText("now playing : "+filename); } if(e.getSource()==pauseButton){ //code for pause button if(player!=null){ try { pause=fileInputStream.available(); player.close(); } catch (IOException e1) { e1.printStackTrace(); } } } if(e.getSource()==resumeButton){ //starting resume thread resumeThread.start(); } if(e.getSource()==stopButton){ //code for stop button if(player!=null){ player.close(); songNameLabel.setText(""); } } } Runnable runnablePlay=new Runnable() { @Override public void run() { try { //code for play button fileInputStream=new FileInputStream(myFile); bufferedInputStream=new BufferedInputStream(fileInputStream); player=new Player(bufferedInputStream); totalLength=fileInputStream.available(); player.play();//starting music } catch (FileNotFoundException e) { e.printStackTrace(); } catch (JavaLayerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }; Runnable runnableResume=new Runnable() { @Override public void run() { try { //code for resume button fileInputStream=new FileInputStream(myFile); bufferedInputStream=new BufferedInputStream(fileInputStream); player=new Player(bufferedInputStream); fileInputStream.skip(totalLength-pause); player.play(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (JavaLayerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }; } |
1 2 3 4 5 |
public class MainClass { public static void main(String[] args){ new MusicPlayer(); } } |
- Now you can run your program and play mp3 files using your Music Player.
- You can also download the source code of this project by clicking on the download link given below.
How to Play Mp3 file in Java Source Code Download
- Here you can download the Java MP3 player Source Code.
- The link of the file is given below.
So, guys, this was all from this tutorial about how to play mp3 file in Java and if you still have any problem then put down your doubts in the comment section. Thank You.
Related Articles
- Login Form in Java Swing with Source Code
- Registration Form in Java with Database Connectivity
- Calculator Program in Java Using Swing/JFrame
- Tic Tac Toe Game in Java with Source code
- Java Text to Speech Tutorial
- How to Connect MySQL Database in Java Using Eclipse
- How to Connect MySQL Database in Java Using NetBeans
Dear Mehtab Hameed
How can I thank you and express my deep appreciation of your excellent style of educational programming. Beautiful is your presentation for creating an MP3 Player in Java.
The undersigned is 78 years old and an experienced programmer, now engaged in teaching programming in Technical Vocational Training Org, Neyshabur, Khorasan Razavi, Iran.
Fortunately you are young and have many decades of active life ahead. I wish health and the best of success, prosperity and happiness for you.
Best Regards
Masood Raji
Iranshahr Math and IT Training Institute
# 194, Soltanabad Jonoobi, Emam Avenue,
Neyshabur, Iran
email: [email protected]
you have done a great job for students like me.. till now i did not find any other better explanation for mp3 player than yours… amazing thank you.. ‘
once again u r amazing
Thank you so much ….keep sharing !
What is the Best YouTube Videos to MP3 Converter ?
You can use any application to convert YouTube videos to MP3
Nice, until you try and do more than 1 thing. It breaks if stopped, paused or restarted. It should be useable for more than 1 song if you are going to share. A lot better than anything I could put together, but using this to fix my own creation did not help much. So, I created a new project and used this as is. Would have been nice to know it didn’t really function before I put 3 hours into this.