2013-06-21 38 views
0

我想停止使用JButton的音頻流,我怎麼能做到這一點?我想讓音頻停止與JButton,我該怎麼做?

這裏是我的代碼:

import sun.audio.AudioPlayer; 
import sun.audio.AudioStream; 

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.InputStream; 

public class twerk { 
Thread thread; 
static int loo = 1; 
static File fl = new File("MYFILE"); 
public static void frame(){ 
    JFrame frame = new JFrame("COLLIN"); 
    frame.setSize(1086,800); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setResizable(false); 
    ImageIcon image = new ImageIcon("MYPICTURE"); 
    JLabel label = new JLabel(image); 
    frame.add(label, BorderLayout.NORTH); 
    frame.setIconImage(image.getImage()); 

    JButton button = new JButton("Stop Sound"); 
    frame.add(button, BorderLayout.SOUTH); 


    frame.setVisible(true); 
} 


    public static void PlayClip(){ 
     try 
     { 

      String st =fl.getPath(); 
      InputStream in = new FileInputStream(st); 
      AudioStream au = new AudioStream(in); 
      AudioPlayer.player.start(au); 
      if(loo == 1){ 
       Thread.sleep(1900); 
       PlayClip(); 
      } 

     } catch(Exception e){} 
    } 



    public static void main(String[] args){ 
     frame(); 
     PlayClip(); 
} 



    } 

我想停止非盟這樣一個ActionListener:

static class Action implements ActionListener{ 
     public void actionPreformed (ActionEvent e){ 
      AudioPlayer.player.stop(); 
     } 

我會怎麼做。也許我可以做類似

if(JButton.clicked){ 
audiostream.player.stop 
} 

任何輸入將不勝感激!

+0

你的歌聲,你的圖形用戶界面在不同的線程運行,則必須中斷其他線程,請遵循Java代碼convetions – nachokk

+0

請注意,我們並不需要在大膽的聲明,以閱讀它們! –

+0

我不明白爲什麼ü具有可變線程你不使用它..你用凍結你的Thread.sleep貴,你做遞歸調用同一個函數.. – nachokk

回答

0

嘗試在您的twerk類中實現ActionListener。然後在你的類中實現actionPerformed方法並將偵聽器添加到按鈕中。例如

public class twerk implements ActionListener{ 
public void twerk() { 
    button.addActionListener(this); 
// omitted 
} 

public void actionPerformed(ActionEvent e) { 
    JButton tmp = (JButton)e.getSource(); 
    if(tmp == button) 
     au.player.stop(); 
} 

並且還這是一個很好的做法,使你的類名開始大寫字母像'Twerk'

與應用程序祝你好運!