【发布时间】:2011-07-29 08:55:55
【问题描述】:
代码
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);
}
}
}
这段代码确实提供了输出 sound thread working ,但不播放任何东西。在这段代码中,我已经启动了一个单独的线程来播放声音,并且在声音线程完成它的工作之前程序不应该终止。但是程序在打印一系列声音线程工作后终止。
这是什么原因(程序终止,声音不播放)?
【问题讨论】:
-
我的意思是编辑你的other question。但是我在那里制作的相同 cmets 在这里适用。链接源中的最后两个单行 cmets 对您意味着什么?
-
@Andrew Thompson 这意味着程序只有在由
clip生成的守护线程完成后才会终止。但我不知道为什么它会在播放 3-4 秒后结束。 -
@ Andrew Thompson
JOptionPane出现一段时间后自行消失。我之前发布过这个问题,但没有得到任何有意义的答案stackoverflow.com/questions/6594921/… -
"
JOptionPane出现一段时间后自行消失。" 我能想到的唯一一件事就是“您的 Java 安装已损坏”。 -
@Andrew Thompson 你说的坏了是什么意思
标签: java multithreading audio javasound