Why does AudioRecord.getMinBufferSize return ERROR_BAD_VALUE (-2)?

吃可爱长大的小学妹 提交于 2019-12-23 07:16:02

问题


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

It returns -2 ERROR_BAD_VALUE.

The native sample rate is 44100Hz, as returned by

AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM).

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.

Update: my Manifest has AUDIO_RECORD as permission.

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


回答1:


From the platform source file 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;
    }
    ...
}

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




回答2:


In emulator it will always return -2. With the same code it will ok on real mobile.



来源:https://stackoverflow.com/questions/4781781/why-does-audiorecord-getminbuffersize-return-error-bad-value-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!