2017-09-27 11 views
0

実装されたRunnableクラス(Soundtrack)から曲を再生するスレッドを作成しました。私のボタン(jMenuItem1ActionPerformed)を押して停止したいと考えています。私はそれをgoogled &停止するために多くの方法を試みたが失敗した私は私のケースでは、それを行う別の方法があると思う。以下、次のコード:Java Swingボタンを押してスレッドを停止する方法はありますか?

public static class Soundtrack implements Runnable { 
    @Override 
    public void run() { 
     try{ 
     File file = new File("SF.mp3"); 
     FileInputStream fis = new FileInputStream(file); 
     BufferedInputStream bis = new BufferedInputStream(fis); 

     try{ 
      Player player = new Player(bis); 
      player.play(); 
     }catch(JavaLayerException ex){} 
    }catch(IOException e){} 
    } 
    } 
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {           
    // TODO add your handling code here: 
}  
public static void main(String args[]) { 
    Thread background = new Thread(new Soundtrack()); 
    background.start(); 
} 
+0

あなたはplayer.stop()、またはそのような何かを呼び出すことができますか? – jwils

+0

私は傾けることはできませんが、player.close()があります。Player player = new Player()のように呼び出すことはできません。 player.close(); ;それでもまだ動作していないのに私はすべての試しとキャッチコマンドを入れなければならなかった、私はPlayerクラスが元Javaからではないと思う。私は新しいライブラリJLayer 1.0.1をhttp://www.javazoom.net/index.shtmlから追加したmp3ファイルを再生するために、これらは私がプレーヤーで設定できるコマンドです。: close(); (Object o)と等しいです。 getClass(); getPosition();ハッシュコード(); isComplete(); notify(); notifyAll();遊びます();遊ぶ(int i); toString();待つ();待機(長いl); wait(long l、int i) – Rafa

答えて

0

私は解決策のための私の姉感謝したい:

public static class Soundtrack implements Runnable { 
    @Override 
    public void run() { 
     try{ 
     File file = new File("SF.mp3"); 
     FileInputStream fis = new FileInputStream(file); 
     BufferedInputStream bis = new BufferedInputStream(fis); 

     try{ 
      Player player = new Player(bis); 
      player.play(); 
     }catch(JavaLayerException ex){} 
    }catch(IOException e){} 
    } 
    } 
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {           
    // TODO add your handling code here: 
     background.stop();  
}           
private static Thread background; 
public static void main(String args[]) { 
    background = new Thread(new Soundtrack()); 
    background.start(); 
} 
+0

正しい。しかし、あなたは[エグゼクティブ](https://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html)と 'Future'について学ぶことで利益を得るかもしれません。 –

関連する問題