用java读取声音时遇到问题

时间:2015-07-09 00:01:17

标签: java audio

我正在使用Java声音从麦克风中读取声音并使用fft进行转换。我遇到的问题是,当我在调用soundline.read()之后检查字节数组时,它只包含0,1和-1。我很困惑。以下是我的代码:

private void setupSound() {
    Vector<AudioFormat> formats = getSupportedFormats(TargetDataLine.class);

    try {
        float sampleRate = 44100;
        int sampleSizeInBits = 16;
        int channels = 1;
        int frameSize = 2;
        boolean signed = false;
        boolean bigEndian = false;
        format =  new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sampleRate, sampleSizeInBits, channels, frameSize, sampleRate, bigEndian);
        soundLine = (TargetDataLine) AudioSystem.getTargetDataLine(format);
        soundLine.open(format);
        soundLine.start();
    } catch (LineUnavailableException ex) {
        System.out.println(ex);
    }
}

private byte[] readSoundSample() {
    byte[] buffer = new byte[44100];
    int len = soundLine.read(buffer, 0, buffer.length);
    return buffer;
}

0 个答案:

没有答案