1

オーディオを再生するスレッドがありますが、フォームを閉じるときに、プレーヤーがオーディオ ファイルを再生するのを停止したい場合、スレッドの実行方法は次のとおりです。

public static Player playplayer;
public static FileInputStream fis;
public static BufferedInputStream bis;
public void run(){
   while(!Thread.currentThread().isInterrupted()){
       try{
          String file="E://Net Beans Work Space//The Imran's Regression"
            + "//src//imranregression//audio//iron man.mp3";
          fis= new FileInputStream(file);
          bis= new BufferedInputStream(fis);
          playplayer = new Player(bis);           
          playplayer.play();            
       }catch(Exception e){
             System.out.print("ERROR "+e);
       }
   } 

私がやっていることを私のプレーヤーを止める私の方法は次のとおりです:

  private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
  try{
      BGMusic.fis.close();
      BGMusic.bis.close();
      BGMusic.playplayer.close();      //want to close my player here
      audioplay.interrupt();   //stopping my thread here

  }catch(Exception e){

      return;
  }
}    

問題は、スレッドを停止したり、オーディオの再生を停止したりするために、私がやりたいことをしていなかったということです。どうすればこれを達成できますか? 助けてくださいよろしくお願いします!

4

2 に答える 2

0

この3行の移動

 BGMusic.fis.close();
 BGMusic.bis.close();
 BGMusic.playplayer.close();      //want to close my player here

閉店直後

 while(!Thread.currentThread().isInterrupted()){
    //stuff
 }

動作するはずです。私の側では、ブール値を作成して、スレッドが終了する必要があるかどうかを確認し、スレッドが終了するjoin()のを待つために使用することを好みます。

于 2013-05-15T15:07:22.100 に答える