1

以下代码在 OSX 上的 Java 6 中运行时按预期工作(它不会发出声音 - 它会多次播放静音样本),但在 Java 7 和 8 中运行时,在每个样本播放结束时,会出现一个点击。无法理解为什么;在 6 和 7 之间看不到任何明显的 API 变化。(这是摩尔斯电码训练程序的一部分,它也播放 dits 和 dahs.. 在每个静音间隙或样本末尾单击显然是不可接受的)

提前感谢您的建议....

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineEvent.Type;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;

// Demonstration of the 'clicking sound' fault in Java 7 & 8
// Repeatedly play a short sample of silence (happens on other samples too).
public class PlaySilence {
    private static int SAMPLE_RATE = 48000;
    private static final AudioFormat FORMAT = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 
            (float) SAMPLE_RATE, 8, 1, 1, SAMPLE_RATE, false);

    public static void main(String[] args) throws LineUnavailableException, InterruptedException {
        AudioListener listener = new AudioListener();
        Clip clip = oneSecondSilentClip();
        System.out.println("Clip is a " + clip);
        clip.addLineListener(listener);

        System.out.println("Starting");
        for (int i = 0; i < 10; i++) {
            listener.reset();

            System.out.println("Playing..." + System.currentTimeMillis());
            clip.setFramePosition(0);
            clip.start(); 
            clip.drain(); // just to make sure we've finished the clip; the listener is more reliable?

            // On OSX Java 1.6.0_65, you just hear silence here as expected.
            // On OSX Java 1.8.0_25 and 1.7.0_75, at the end of each playback of the clip, there's a
            // very noticeable click.

            listener.waitUntilDone();
        }
        System.out.println("Done");
        clip.removeLineListener(listener);
        clip.close();
    }

    private static Clip oneSecondSilentClip() throws LineUnavailableException {
        int samples = (int) (SAMPLE_RATE * (double) 1);
        byte[] out = new byte[samples];
        Clip clip = AudioSystem.getClip();
        clip.open(FORMAT, out, 0, out.length);
        clip.setFramePosition(0);
        return clip;
    }

    // Adapted from http://stackoverflow.com/questions/577724/trouble-playing-wav-in-java/577926#577926
    // Thanks, McDowell.
    static class AudioListener implements LineListener {
        private boolean done = false;

        public synchronized void reset() {
            done = false;
        }

        public synchronized void update(LineEvent event) {
            Type eventType = event.getType();
            System.out.println("event " + eventType);
            if (eventType == Type.STOP || eventType == Type.CLOSE) {
                done = true;
                notifyAll();
            }
        }

        public synchronized void waitUntilDone() throws InterruptedException {
            while (!done) {
                wait();
            }
        }
    }

}
4

1 回答 1

1

它不喜欢音频格式中的帧大小为 1,任何大于 1 的内容似乎都可以正常工作:

private static int FRAME_SIZE = 2;

private static final AudioFormat FORMAT = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
      SAMPLE_RATE, 8, 1, FRAME_SIZE, SAMPLE_RATE, false);
于 2016-02-01T08:21:39.093 回答