作为参考,该示例
close()通常在Java 5或6下使用退出。
除非已在初始线程上正常关闭Java 5和6,否则在EDT上调用
stop()而不是
close()Java会同时挂起Java 5和6
line。这似乎是
drain()阻塞的预期结果,因为停止的线路无法耗尽。
import java.awt.EventQueue;import javax.sound.sampled.AudioFormat;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.LineUnavailableException;import javax.sound.sampled.SourceDataLine;import javax.swing.JOptionPane;public class Tone { public static void main(String[] args) throws LineUnavailableException { final AudioFormat af = new AudioFormat(Note.SAMPLE_RATE, 8, 1, true, true); final SourceDataLine line = AudioSystem.getSourceDataLine(af); EventQueue.invokeLater(new Runnable() { public void run() { JOptionPane.showMessageDialog(null, "Halt"); //line.stop(); // stops and hangs on drain line.close(); } }); line.open(af, Note.SAMPLE_RATE); line.start(); for (Note n : Note.values()) { play(line, n, 500); play(line, Note.REST, 10); } line.drain(); line.close(); } private static void play(SourceDataLine line, Note note, int ms) { ms = Math.min(ms, Note.SEConDS * 1000); int length = Note.SAMPLE_RATE * ms / 1000; int count = line.write(note.data(), 0, length); }}需要
Note。



