How to build your own mp3 player in Java

So guys i’ll show the way of how to play mp3 files and all other functions related to a mp3 player.So first i’ll show you my own mp3 player.Hope that will motivate you to come up with your own MP3 player.So here we goo..

Screen Shot 2558-11-15 at 1.48.48 PM

This is my mp3 player so first you’ll have to make your GUIs using photoshop first.

Add the created UI to netbeans as icon in a Label and infant of that main table you’ll have to make some other labels to all the buttons.So we’ll not use buttons at all but the labels.If you want you can use the buttons from netbeans or whatever the IDE you want.But if you want this mp3 player as more colourful then you’ll need to use labels.

So now i’ll show how to code for all these buttons(Actually labels) and related functions.

Please make sure you have installed “jl1.0.jar” to your libraries before you start your implementations.Because it is required to play a song in player class.

  • Here are the classes i have imported and the initialised variables/objects 

import java.io.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javazoom.jl.player.Player;
import javazoom.jl.decoder.JavaLayerException;

public class MainClass 
{
    FileInputStream FIS;
    BufferedInputStream BIS;
 
    public Player player;
    public long pauseLocation;
    public long songTotalLength; 
    public String fileLocation;

Then i implemented all above methods inside the main method.

  • Play method 

Here i have used file input stream and buffered input stream and Player class to read and play the audio files.


public void Play(String path){
    try {
       FIS = new FileInputStream(path);
       BIS = new BufferedInputStream(FIS);
 
       player = new Player(BIS); 
       songTotalLength = FIS.available();
       fileLocation = path + "";
 
    }catch (FileNotFoundException | JavaLayerException ex) {
 
    } catch (IOException ex) {
      }
    new Thread(){
    @Override
    public void run(){
       try{
           player.play();
 
           if(player.isComplete()MP3PlayerGUI1.count == 1){
           Play(fileLocation);
       }
 
     if(player.isComplete()){
     MP3PlayerGUI1.Display.setText("");
     }
    }
    catch(JavaLayerException ex){
    }
   }
 }.start();
}

Display is a variable name of the Label which i used for print the name of the song which is playing and MP3PlayerGUI1 is my jframe.

  • Pause method

public void Pause(){
     if(player != null){
         try {
             pauseLocation = FIS.available();
             player.close();
         } catch (IOException ex) {
         }
     }
}

  • Resume method

public void Resume(){
   try {
   FIS = new FileInputStream(fileLocation);
   BIS = new BufferedInputStream(FIS);
 
   player = new Player(BIS);
   FIS.skip(songTotalLength - pauseLocation);
 
      } catch (FileNotFoundException | JavaLayerException ex) {
 
      } catch (IOException ex) {
   }
   new Thread(){
   @Override
      public void run(){
        try{
            player.play();
        }
        catch(JavaLayerException ex){
 
        }
   }
 }.start();
}

  • Stop method

public void Stop(){
     if(player != null){
         player.close();
         pauseLocation = 0;
         songTotalLength = 0;
         MP3PlayerGUI1.Display.setText("");
     }
}

So that’s it with the main coding parts.
Actually i used lots of methods to make this player more colourful and user friendly.So i used some background wallpaper changer,intro animation and i removed the panel header and implemented my own close,minimise buttons as well.

So for now i hope now you can build your own simple mp3 player.

Here is the link of my Github repository of MyOwnMp3player.So that you canlook at my code or even download and customise as you want.:)

9 thoughts on “How to build your own mp3 player in Java

Leave a comment