.wavファイルを開いてクリップを使用して再生しようとしています。しかし、私がmyClip.open(...)を呼び出すと、スレッドはフリーズし、決して再開しません。エラーはスローされません。Java clip.openが無期限にハングアップする
try {
AudioInputStream mySound = AudioSystem.getAudioInputStream(new File("sounds/myAudioFile.wav"));
myClip = AudioSystem.getClip();
myClip.open(mySound); //This is where it hangs
myClip.start(); //This is never executed
} catch (Exception e) {e.printStackTrace();}
EDIT: 私のコードの代替バージョン(も動作しません):
Clip myClip = null;
new Thread(new Runnable() {
public void run() {
try {
AudioInputStream mySound = AudioSystem.getAudioInputStream(new File("sounds/myAudioFile.wav"));
myClip = AudioSystem.getClip();
System.out.println("Before open");
myClip.open(mySound); //This is where it hangs
System.out.println("After open"); //never executed
//This thread runs indefinitely, whether or not clip.start() is called
} catch (Exception e) {e.printStackTrace();}
}
}).start();
try{ Thread.sleep(1000); }catch(Exception e){} //give it a second to open
myClip.start(); //Executed, but doesn't make a sound
System.out.println("Started clip");
出力:
Before open
Started clip
ここに私のコードのシンプルなバージョンがあります
私はこれを引き起こす原因を知っていますが、スレッドが永遠に凍結する方法を理解することはできません。コンピュータのサウンドドライバが動作を停止することがあり、プログラムからのサウンドが再生されないようにするため、フリーズします。私は、clip.openメソッド(またはスレッド)を強制的に2〜3秒後にタイムアウトさせる何らかの方法が必要です。 別のスレッドのclip.start()の呼び出しは機能しますが、クリップが開かれていないためサウンドは再生されません。 clip.open(...)を含むスレッドは、clip.start()を呼び出した後も永遠に実行されます。
スレッドで書き込もうとします。それ以外の場合、メインスレッドはブロックされ、フリーズするように感じます。 – Boola