MP3 Player in Java

Posted on the 09 May 2014 by Savio Menezes
Hello everyone. I have done simple mp3 player using Java programming language. For this I have used ecplise IDE with windowBuilder addon and JLayer. My mp3 player looks something like this
This project contains two .java files. Code of each file is given below:
MP3Player.java
package savmp3;
import java.awt.Color;
@SuppressWarnings("serial")
public class Mp3Player extends JFrame {
   private JPanel contentPane;
   JButton btnPause;
   JButton btnChooseFile;
   JButton btnResume;
   JButton btnStop;
   JLabel lblName;
   Mp3PlayerWorking mw;
     
   /**
    * Launch the application.
    */
   public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            try {
               Mp3Player frame = new Mp3Player();
               frame.setVisible(true);
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      });
   }
   /**
    * Create the frame.
    */
  
   public Mp3Player() {
      //assignments
      contentPane = new JPanel();
      btnPause = new JButton("pause");
      btnChooseFile = new JButton("open");
      btnResume = new JButton("resume");
      btnStop = new JButton("stop");
      lblName = new JLabel("");
     
     
      //features of Mp3Player frame
      setTitle("savmp3");
      setResizable(false);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setBounds(100, 100, 392, 119);
     
      //Panel in Mp3Player frame
      contentPane.setBackground(new Color(102, 153, 51));
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      setContentPane(contentPane);
      contentPane.setLayout(null);
     
      //pause button
      btnPause.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent arg0) {
            mw.pause();
            btnChooseFile.setEnabled(false);
            btnResume.setEnabled(true);
            btnPause.setEnabled(false);
            btnStop.setEnabled(false);
            }});
      btnPause.setBackground(Color.WHITE);
      btnPause.setBounds(104, 12, 80, 25);
      btnPause.setEnabled(false);
      contentPane.add(btnPause);
     
            //Choose files button
            btnChooseFile.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent arg0) {
                  //open file dialog
                  JFileChooser fileChoose = new JFileChooser();
                                   
                  //only mp3 files should appear in file dialog
                  FileNameExtensionFilter filter = new FileNameExtensionFilter("MP3 Files","mp3");
                  fileChoose.setFileFilter(filter);
                  int val= fileChoose.showOpenDialog(Mp3Player.this);
                 
                  //display selected file in label
                  if(val==JFileChooser.APPROVE_OPTION)
                  {
                     mw = new Mp3PlayerWorking(fileChoose.getSelectedFile());
                    
                     lblName.setText(fileChoose.getSelectedFile().getName());
                    
                    
              
              
               btnPause.setEnabled(true);
               btnResume.setEnabled(false);
               btnChooseFile.setEnabled(false);
               btnStop.setEnabled(true);
              
               mw.play(-1);
  
                  }}
              
            });
            btnChooseFile.setBackground(Color.WHITE);
            btnChooseFile.setBounds(12, 12, 80, 25);
            contentPane.add(btnChooseFile);
           
            //resume button
            btnResume.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent arg0) {
                 
                  btnChooseFile.setEnabled(false);
                  btnPause.setEnabled(true);
                  btnResume.setEnabled(false);
                  btnStop.setEnabled(true);
                  mw.resume();
               }
            });
            btnResume.setEnabled(false);
            btnResume.setBackground(Color.WHITE);
            btnResume.setBounds(196, 12, 87, 25);
            contentPane.add(btnResume);
           
            //stop button
            btnStop.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent arg0) {
                  lblName.setText(" ");
                  mw.stop();                 
                  btnChooseFile.setEnabled(true);
                  btnPause.setEnabled(true);
                  btnResume.setEnabled(false);
                  btnStop.setEnabled(true);
               }
            });
            btnStop.setBackground(Color.WHITE);
            btnStop.setForeground(Color.BLACK);
            btnStop.setBounds(295, 12, 80, 25);
            btnStop.setEnabled(false);
            contentPane.add(btnStop);
           
            //label to display file name
            lblName.setFont(new Font("Dialog", Font.BOLD, 10));
            lblName.setHorizontalAlignment(SwingConstants.LEFT);
            lblName.setForeground(Color.WHITE);
            lblName.setBounds(12, 46, 363, 37);
            contentPane.add(lblName);
                       
   }
   }

MP3PlayerWorking.java
package savmp3;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
public class Mp3PlayerWorking {
   Player player;
   FileInputStream fis;
   BufferedInputStream bis;
   long pauseLocation;
   long totalSongLength;
   File selectedFile ;
  
   public Mp3PlayerWorking(File file)
   {
      player=null;
      fis = null;
      bis = null;
      pauseLocation = 0;
      totalSongLength =0;
      selectedFile = file;
   }
  
   public void pause()
   {
      try {
         pauseLocation=fis.available();
        
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      player.close();
   }
  
  
   public void play(long position)
   {
      try {
         fis = new FileInputStream(selectedFile);
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      try {
         totalSongLength=fis.available();
      } catch (IOException e) {
         // TODO Auto-generated catch block
      e.printStackTrace();
      }
     
      if(position > -1)
      {
         try {
            fis.skip(position);
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }
     
      bis = new BufferedInputStream(fis);
      try {
         player = new Player(bis);
      } catch (JavaLayerException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
     
     
     
      new Thread()
         {
            public void run()
            {
               try {
                     player.play();
               }    catch (JavaLayerException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                  }
            }
          }.start();
        
   }
   public void resume()
   {
      if(fis!=null)
      {
         play(totalSongLength-pauseLocation);
      }
   }
  
   public void stop()
   {
      if(player!=null)
      {
         player.close();
         totalSongLength=0;
         pauseLocation=0;
      }
   }
}

 Here is a executable jar file of this player. Download it here. Make sure that you have installed Java on your computer.