我不确定为什么您链接的第二种方法会启动另一个线程;我相信音频还是会以自己的方式播放。剪辑播放完毕之前,您的应用程序完成了问题吗?
import javax.sound.sampled.*;import java.io.File;import java.io.IOException;import javax.sound.sampled.LineEvent.Type;private static void playClip(File clipFile) throws IOException, UnsupportedAudioFileException, LineUnavailableException, InterruptedException { class AudioListener implements LineListener { private boolean done = false; @Override public synchronized void update(LineEvent event) { Type eventType = event.getType(); if (eventType == Type.STOP || eventType == Type.CLOSE) { done = true; notifyAll(); } } public synchronized void waitUntilDone() throws InterruptedException { while (!done) { wait(); } } } AudioListener listener = new AudioListener(); AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(clipFile); try { Clip clip = AudioSystem.getClip(); clip.addLineListener(listener); clip.open(audioInputStream); try { clip.start(); listener.waitUntilDone(); } finally { clip.close(); } } finally { audioInputStream.close(); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)