2013-01-15 33 views
-1

我使用此程序在java中播放mp3文件。 下面的代碼播放的MP3文件,但在播放時不能停...... 我加了兩個獨立的ActionListener兩個按鈕,但它不會適用於停止按鈕...如何在兩個不同的線程中爲以下代碼運行ActionListenters

import java.io.*;  
import javax.swing.*; 
import java.awt.event.*; 
import javazoom.jl.player.Player; 
import java.io.FileInputStream; 



    public class Play2 extends JFrame 
    { 
    JButton b,b1; 
    JTextField t; 

    Play2() 
    { 
     JFrame j=new JFrame("MusicPlayer"); 
     j.setSize(300,300); 
     j.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     b=new JButton("Play"); 
     b1=new JButton("Stop"); 
     JPanel p=new JPanel(); 
     t=new JTextField(20); 
     p.add(t); 
     p.add(b); 
     p.add(b1); 
     j.add(p); 
     j.setVisible(true); 


    } 
    public void awt() 
    { 
    b.addActionListener(
      new ActionListener(){ 
        public void actionPerformed(ActionEvent ae){ 
       try 
        { 

      String fname=t.getText(); 
      File directory = new File(fname); 

      boolean isDirectory = directory.isDirectory(); 

      if (isDirectory) 
      { 
        // It returns true if directory is a directory. 
       System.out.println("the name you have entered is a directory : " + directory); 
        //It returns the absolutepath of a directory. 
        System.out.println("the path is " + directory.getAbsolutePath()); 
      } 
      else 
      { 
        // It returns false if directory is a file. 
       System.out.println("the name you have entered is a file : " + directory); 
        //It returns the absolute path of a file. 
        System.out.println("the path is " + directory.getAbsolutePath()); 
      } 
       String s=directory.getAbsolutePath(); 

       s=s.replace("\\","/") ; 

        FileInputStream fis=new FileInputStream(s); 
        final Player playMp3=new Player(fis); 

        playMp3.play(); 
     } 
     catch(Exception e){ 
      System.out.println(e);} 
     }//end actionPerformed 
     }//end ActionListener 
    );//end addActionListener() 

    b1.addActionListener(
     new ActionListener(){ 
     public void actionPerformed(
            ActionEvent ae){ 
      //Terminate playback before EOF 
        try 
        { 

      String fname=t.getText(); 
      File directory = new File(fname); 

      boolean isDirectory = directory.isDirectory(); 

      if (isDirectory) 
      { 
        // It returns true if directory is a directory. 
       System.out.println("the name you have entered is a directory : " + directory); 
        //It returns the absolutepath of a directory. 
        System.out.println("the path is " + directory.getAbsolutePath()); 
      } 
      else 
      { 
        // It returns false if directory is a file. 
       System.out.println("the name you have entered is a file : " + directory); 
        //It returns the absolute path of a file. 
        System.out.println("the path is " + directory.getAbsolutePath()); 
      } 
       String s=directory.getAbsolutePath(); 

       s=s.replace("\\","/") ; 

        FileInputStream fis=new FileInputStream(s); 
        final Player playMp3=new Player(fis); 

        playMp3.close(); 
     } 
     catch(Exception e){ 
      System.out.println(e);} 
     }//end actionPerformed 
     }//end ActionListener 
    );//end addActionListener() 

    } 


     public static void main(String[]args) 
     { 
     Play2 f=new Play2(); 
     f.awt();  
    } 

} 
+0

'最終球員了playMP3 =新的(你在閱讀本特別的信息不會有延遲的EDT部分,其中包含有關這一具體問題的信息會很有趣)播放器(FIS); playMp3.play();'playMp3'應聲明爲可用於任何需要它的方法的類的屬性,例如,一個'start()'或'stop()'方法。 –

+0

請使用一致和一致的縮進。就目前而言,你的代碼很難閱讀(並且非常容易出錯)! –

回答

0

你在做什麼這裏是你的播放按鈕監聽器使用EDT(Event Dispatch Thread)。

當您單擊「播放」按鈕時,ActionEvent將被髮送到事件隊列。如果您隨後按下「停止」按鈕,則另一個ActionEvent將被髮布到EventQueue。

EDT將開始處理事件隊列(播放事件)中的第一個事件。在這種情況下,您可以在EDT上的(mp3)Player類中調用play()方法。 play()方法持續循環播放,直到播放完整個mp3。在整個過程中,其他事件(停止事件)將在事件隊列中輪候執行。所以當它最終成爲停止事件輪流執行時,mp3已經播放,直到結束,停止命令相當多。


的主要問題:你不應該在美國東部時間執行冗長的任務。

A液:確保播放方法是通過使用SwingWorker class調用Player.play()方法在非EDT線程執行。

參考Event-Dispatch Thread Rules for Swing UIs (online article)

相關問題