简体   繁体   English

在Java问题中使用JLayer在线程中仅播放一个mp3文件

[英]Playing only one mp3 file in a thread using JLayer in java issue

I'm working at an Audio Player, which is written in Java with GUI. 我在一个音频播放器上工作,该音频播放器使用Java和GUI编写。 For playing the mp3 files, I've chosen JLayer library from javazoom because I saw it's very popular and used. 为了播放mp3文件,我从javazoom选择了JLayer库,因为我看到它非常流行和使用。 I made the GUI, managed to play the selected mp3 file from the playlist. 我制作了GUI,设法播放了从播放列表中选择的mp3文件。

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: 这是我播放mp3文件的代码:

    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. 如果我无法使用JLayer做到这一点,请向我推荐一个很好的库,可以用来播放mp3文件。 Thank you in advance and I'm waiting for your suggestions. 预先谢谢您,我正在等待您的建议。

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. 因此,主要思想是,当我开始播放歌曲时,该int将取值为1,并且不会为0,因此无法再创建线程。

按下播放按钮后使用此功能,一旦播放器启动按钮,线程就已经启动,一旦完成歌曲,该按钮将禁用并再次启用

    yourplaybutton.setEnabled(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM