Search code examples
androidandroid-5.0-lollipoprecordaudiorecord

Android Studio Supported PCM Encoding Formats


I am trying to record Audio using the AudioRecord class with different sample rates and PCM encodings, the problem is, when constructing the AudioRecord object and implementing the AudioRecorder.getMinBufferSize() method using AudioFormat.ENCODING_PCM_8BIT or AudioFormat.ENCODING_PCM_FLOAT, different errors are reported such as:

Unsupported sample encoding. Should be
ENCODING_PCM_8BIT
or ENCODING_PCM_16BIT.

or

E/android.media.AudioRecord: getMinBufferSize(): Invalid audio format.

The thing is, I am testing on a Moto G first generation with Lollipop, so according to the Android Developer documentation AudioFormat.ENCODING_PCM_FLOAT is supported. Apparently AudioRecorder.getMinBufferSize() only works with AudioFormat.ENCODING_PCM_16BIT.

So how am I supposed to know the MinBufferSize to implement on the constructor?

AudioRecord constructor:

audioRecorder = new AudioRecord(audioSource, sampleRate, channelConfig, audioFormat, mBufferSize);

AudioRecord method:

mBufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);

Solution

  • AudioRecord and AudioTrack are 2 different things.

    AudioFormat.ENCODING_PCM_FLOAT is supported by AudioTrack since Lollipop

    But AudioRecord supports AudioFormat.ENCODING_PCM_FLOAT only since Marshmallow.

    Check http://developer.android.com/reference/android/media/AudioFormat.html.

    enter image description here