このコードで.wavファイルを再生しようとしています:
public class AudioTest {
public AudioTest() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
System.out.println(classLoader.getResourceAsStream("sound.wav"));//to see if the sound file is found
try{
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(classLoader.getResourceAsStream("sound.wav"));
clip.open(inputStream);
clip.start();
while (!clip.isRunning())
Thread.sleep(10);
while (clip.isRunning())
Thread.sleep(10);
clip.close();
} catch (Exception e)
{
System.out.println("something failed");
}
System.out.println("done"); //to see if the sound is finished playing
}
}
これは機能しますが、私が去った場合:
while (!clip.isRunning())
Thread.sleep(10);
while (clip.isRunning())
Thread.sleep(10);
clip.close();
一部の音は再生されません。私が見た.wavファイルの再生のほとんどすべての例にはその部分がありませんでした。何が問題になり、これを修正するためのよりエレガントなワットがありますか?