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

由网友(黑色の薔薇)分享简介:我在三星Galaxy S I9000测试这一点。I am testing this on a Samsung Galaxy S i9000.int sampleRate = 44100;int bufferSize = AudioRecord.getMinBufferSize(sampleRate, AudioF...

我在三星Galaxy S I9000测试这一点。

I am testing this on a Samsung Galaxy S i9000.

int sampleRate = 44100;
int bufferSize = AudioRecord.getMinBufferSize(sampleRate, 
    AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_8BIT);

它返回-2 ERROR_BAD_VALUE

本机采样速率为44100Hz,如

The native sample rate is 44100Hz, as returned by

AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM)。

AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM).

我已经尝试设置采样率到1000,8000,22100和44100。我也试图改变 AudioFormat.CHANNEL_IN_MONO AudioFormat.CHANNEL_CONFIGURATION_MONO 。我也曾尝试 STEREO (均 IN_STEREO CONFIGURATION_STEREO ) 。我也曾尝试16位编码,而不是8位。

I have tried setting sampleRate to 1000, 8000, 22100 and 44100. I have also tried changing AudioFormat.CHANNEL_IN_MONO to AudioFormat.CHANNEL_CONFIGURATION_MONO. I have also tried STEREO (both IN_STEREO and CONFIGURATION_STEREO). I have also tried 16 bit encoding instead of 8 bit.

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

Update: my Manifest has AUDIO_RECORD as permission.

我不断收到-2结果。为什么会出现这种情况?

I keep getting -2 as a result. Why is this happening?

推荐答案

从平台源文件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;
    }
    ...
}

看起来像你的选择是16位还是什么都没有。 :

Looks like your choice is 16-bit or nothing. :

阅读全文

相关推荐

最新文章