import javax.sound.sampled.*;
import java.io.*;
public class Tester {
static Thread th;
public static void main(String[] args) {
startNewThread();
while(th.isAlive() == true) {
System.out.println("sound thread is working");
}
}
public static void startNewThread() {
Runnable r = new Runnable() {
public void run() {
startPlaying();
}
};
th =new Thread(r);
th.start();
}
public static void startPlaying() {
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(new File("d:/UnderTest/wavtester.wav"));
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.loop(-1); // keep playing the sound
} catch(Exception exc) {
System.out.println(exc);
}
}
}
此代码确实给输出声音线程工作,但不玩什么。在这段代码中,我已经启动了一个单独的线程来播放声音,程序不应该终止,直到音频线程完成它的工作。但程序终止后打印系列音效线程工作。
这是什么原因(程序终止和声音不播放)?
我的意思编辑[* *其他问题(http://stackoverflow.com/questions/6863384/understanding-the-constructor-of-audioformat-audioinputstream -and - 开始 - 方法/)。但是我在那里发表的同样的评论在这里适用。链接源代码中的最后两条单行注释对您意味着什么? –
@ Andrew Thompson这意味着程序只有在由'clip'生成的守护线程完成后才会终止。但是我不知道为什么它在3-4秒之后完成。 –
@ Andrew Thompson'JOptionPane'出现一段时间后自行消失。我之前发布了这个问题,但没有得到任何有意义的答案http://stackoverflow.com/questions/6594921/how-does-boolean-here-become-false-on-its-own –