在Java播放器中播放和暂停按钮

时间:2016-06-26 14:23:15

标签: java eclipse

我按下" Play"创建了一个播放音乐的程序。按钮但我之后无法做任何事情,我必须等待歌曲结束,直到我可以关闭程序。所以我的问题是,如何制作一个可以在播放时结束歌曲的按钮? PS。我知道代码有点混乱。我还在学习。

package mp3player;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;

import javax.swing.JButton;
import javax.swing.JFrame;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

public class mp3player extends JFrame {
    private JButton play;
    private JButton pause;
    private Player player;

    public mp3player(){

        play = new JButton("Play");
        add(play);
        pause = new JButton("Pause");
        add(pause);

        setLayout(new FlowLayout());
        setSize(300,500);

        Handlerclass handler = new Handlerclass();
        Stopclass stop = new Stopclass();
        play.addActionListener(handler);
        pause.addActionListener(stop);
        try{
        FileInputStream fis = new FileInputStream("C:\\x\\y\\z.mp3");
        player = new Player(fis);
        }catch(Exception e){System.out.println(e);}


        }


    private class Handlerclass implements ActionListener{
        public void actionPerformed (ActionEvent event){
            try {
                player.play();
            } catch (JavaLayerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }


    }

    private class Stopclass implements ActionListener{
    public void actionPerformed (ActionEvent event){
        player.close();
    }
    }   



public static void main(String[]args){

    mp3player mp3 = new mp3player();
    mp3.setDefaultCloseOperation(EXIT_ON_CLOSE);
    mp3.setVisible(true);
}

}

0 个答案:

没有答案