How to Play Mp3 File in Java Tutorial | Simple Steps

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.

   Download jlayer Jar File

  • 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.
  • 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.
  • Now Run your Program.
How to Play Mp3 file in Java
How to Play Mp3 file in Java-fig-1
  • 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.
  • 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.
  • Now you can run your program and play mp3 files using your Music Player.
How to Play Mp3 file in Java
How to Play Mp3 file in Java-fig-2
  • 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.

Download Source Code

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

6 thoughts on “How to Play Mp3 File in Java Tutorial | Simple Steps”

  1. 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]

    Reply
  2. 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

    Reply
      • 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.

        Reply

Leave a Comment