2012-05-26 106 views
0

我正在用java製作一個簡單的MP3Player。我已經設法播放一個.mp3文件,但是當我開始播放整個程序時,我無法點擊程序中的任何按鈕。在我開始播放後,我需要幫助製作暫停按鈕或其他任何按鈕。這是我的代碼:Java MP3Player使用Javazoom播放和暫停

import java.awt.*; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.io.InputStream; 
    import java.net.URL; 
    import javazoom.jl.player.advanced.AdvancedPlayer; 
    import javax.swing.*; 

    public class MP3Player extends JFrame{ 

    public MP3Player(){ 
     JPanel jpBottom = new JPanel(); 
     JButton btnPlay = new JButton("Play"); 
     JButton btnPause = new JButton("Pause"); 

     jpBottom.add(btnPause); 
     jpBottom.add(btnPlay); 

     Container cp = this.getContentPane(); 
     BorderLayout bl = new BorderLayout(); 
     cp.setLayout(bl); 
     cp.add(jpBottom, BorderLayout.SOUTH); 

     btnPlay.addActionListener(
       new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         Play("file:///C://a.mp3"); 
        } 
       } 
     ); 

     this.setVisible(true); 
     this.setSize(250, 100); 
     this.setTitle("MP3 Player"); 
     this.setLocation(100, 100); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    public void Play(String path){ 
     try{ 
      URL url = new URL(path); 
      InputStream in = url.openStream(); 
      //Player pl = new Player(in); 
      //pl.play(); 
      AdvancedPlayer pl = new AdvancedPlayer(in); 

      pl.getPlayBackListener(); 
      pl.play();    
     } 
     catch(Exception e){ 
      System.out.println("Feil: "+e); 
     } 
    } 

    public static void main(String[] args) { 
     MP3Player n = new MP3Player(); 
    } 
} 

回答

1

我需要別人幫助的暫停按鈕,..

的方法暫停播放器是stop()。但是,該代碼創建方法的本地AdvancedPlayer。相反,該類需要聲明AdvancedPlayer屬性並在actionPerformed()方法中引用該類屬性。