为什么AudioRecord.getMinBufferSize返回ERROR_BAD_VALUE(-2)?

Tom*_*Tom 11 android audio-recording

我在三星Galaxy S i9000上测试它.

int sampleRate = 44100;
int bufferSize = AudioRecord.getMinBufferSize(sampleRate, 
    AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_8BIT);
Run Code Online (Sandbox Code Playgroud)

它返回-2 ERROR_BAD_VALUE.

原始采样率为44100Hz,由返回

AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM).

我已经尝试将sampleRate设置为1000,8000,22100和44100.我也尝试过更改AudioFormat.CHANNEL_IN_MONOAudioFormat.CHANNEL_CONFIGURATION_MONO.我也试过STEREO(两个IN_STEREOCONFIGURATION_STEREO).我也尝试过16位编码而不是8位.

更新:我的清单有AUDIO_RECORD权限.

结果我一直得-2.为什么会这样?

Reu*_*ton 18

从平台源文件AudioRecord.java:

static public int getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat) {
    ...
    // PCM_8BIT is not supported at the moment
    if (audioFormat != AudioFormat.ENCODING_PCM_16BIT) {
        loge("getMinBufferSize(): Invalid audio format.");
        return AudioRecord.ERROR_BAD_VALUE;
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

看起来你的选择是16位或没有.:\

  • +1.他说他也尝试了16位,因此该方法中唯一可以在getMinBufferSize中返回ERROR_BAD_VALUE的地方是第472-475行. (2认同)