2017-09-27 11 views
0

구현 된 Runnable 클래스 (Soundtrack)에서 노래를 재생하는 Thread를 만들었으므로 버튼 (jMenuItem1ActionPerformed)을 눌러 중지해야합니다. 나는 그것을 googled & 중지하는 여러 가지 방법을 시도했지만 실패 내 생각에는 거기에 또 다른 방법이있다. 아래에 다음 코드 :자바 스윙 버튼을 눌러 스레드를 멈추는 방법은 무엇입니까?

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(); 나는 모든 try와 catch 명령을 써야했지만 여전히 작동하지는 않습니다. 플레이어 클래스는 원래 자바에서 생성 된 것이 아닙니다. http://www.javazoom.net/index.shtml에서 새 라이브러리 JLayer 1.0.1을 추가했습니다. mp3 파일을 재생하기 위해, 다음 플레이어에서 설정할 수있는 명령입니다. : close(); equals (Object o); getClass(); getPosition(); 해시 코드(); isComplete(); notify(); notifyAll(); 놀이(); 놀이 (int i); toString(); 기다림(); 기다림 (long 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

수정하십시오. 그러나 [executors] (https://docs.oracle.com/javase/tutorial/essential/concurrency/executors.html) 및 'Future'에 대해 학습하면 도움이 될 수 있습니다. –