麻烦在Java中播放wav

我正试着玩

PCM_UNSIGNED 11025.0 Hz, 8 bit, mono, 1 bytes/frame 

文件如此处(1)和此处(2)所述 。

第一种方法有效,但我不想依赖sun.*东西。 第二个导致只播放一些领先的帧,听起来更像是一个点击。 因为我正在使用ByteArrayInputStream播放,所以不能成为IO问题。

Plz分享你为什么会发生这种情况的想法。 TIA。

我不确定为什么你链接的第二种方法会启动另一个线程; 我相信无论如何音频都将在自己的线程中播放。 在剪辑播放完毕之前,您的应用程序是否完成了问题?

 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(); } }