0

I'm working at an Audio Player, which is written in Java with GUI. For playing the mp3 files, I've chosen JLayer library from javazoom because I saw it's very popular and used. I made the GUI, managed to play the selected mp3 file from the playlist.

My problem is that if I press many times on the play button or on the file from the playlist it will start playing the song as many times as I press it and I want to play it one the same thread ; if I press again play button I want to play again not to start the same song while the current one is playing .

Here is my code which play the mp3 file:

    public class Playing implements Runnable{

        private Player mp3Player;
        private Thread playerThread;

        public void createPlayer(FileInputStream file) throws JavaLayerException{

            mp3Player = new Player(file);

            playerThread = new Thread(this);
            playerThread.start();
    }

   @Override
   public void run(){

        try {

            mp3Player.play();
        } 
        catch (JavaLayerException ex) {

            Logger.getLogger(Playing.class.getName()).log(Level.SEVERE, null, ex);
        }
   }

This is my method for the play button:

    public void createPlayButton(){

        play = new JButton();
        playButton = new ImageIcon("D:/Audio Player/Images/playButton.png");
        play.setBounds(125, 100, 50, 50);
        play.setIcon(playButton);
        play.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            for (int i = 0; i < select.getFilesPath().size(); i++){

                if (select.getFilesPath().get(i).toString().contains(playlistBody.getSongName())){

                    try {
                        mp3Player.createPlayer(new FileInputStream(new File(select.getFilesPath().get(i).toString())));
                    } catch (JavaLayerException ex) {
                        Logger.getLogger(PlayerBody.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(PlayerBody.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            }

        }
    });
}

I mention that I'm new to multithreading, so dont be so hard on me. If I cannot do this with JLayer, please recommend me a good library with which I can play mp3 files. Thank you in advance and I'm waiting for your suggestions.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Survivor
  • 674
  • 1
  • 7
  • 13
  • *"please recommend me a good library with which I can play mp3 files."* You only need Java Sound and an MP3 SPI to play MP3 format files. See the [Java Sound tag Wiki](http://stackoverflow.com/tags/javasound/info) for more details. – Andrew Thompson Mar 09 '13 at 16:19
  • And what advantages do I have by using Java Sound instead of JLayer? – Survivor Mar 09 '13 at 22:04
  • By only using Java Sound you have full control over everything and can add support for more than mp3 files just by adding an spi. But to be honest, pure Java Sound can be quite complicated for starters. – Martin Braun Mar 10 '13 at 16:16
  • I guess I'll give it a shot since nobody can give me an hint of what I should do with the thread problem :) Thank you both . – Survivor Mar 10 '13 at 21:37

2 Answers2

0

I fixed my issue with the threads; I'll put the solution, maybe will help someone.

     static int fileRunning = 0;

     public void playMp3(FileInputStream file) throws JavaLayerException{

         if (fileRunning == 0){

             mp3Player = new Player(file);

             playerThread = new Thread(this);
             playerThread.start();

             fileRunning = 1;

         }
     }

So the main idea is that when I start playing a song, that int will take value 1, and it won't be 0, so no more threads can be made.

Survivor
  • 674
  • 1
  • 7
  • 13
0

use this after you press you play button and the thread has been started once player start the button will disable and let it enable again once the song is being completed

    yourplaybutton.setEnabled(false);
Tech Nerd
  • 822
  • 1
  • 13
  • 39