Reputation: 58063
Android: I want to read buffers from mic so that i can perform process on it, Following is my code
int sampleRateInHz = 8000;// 44100, 22050 and 11025
int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
//int bufferSize =11025 +
int bufferSize = AudioRecord.getMinBufferSize(sampleRateInHz,channelConfig, audioFormat);
short[] buffer = new short[bufferSize];
AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz,channelConfig, audioFormat, bufferSize);
if(audioRecord.getState()== AudioRecord.STATE_INITIALIZED){
audioRecord.startRecording();
Log.e("recording", "before");
boolean flag = true;
while (flag) {
int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
System.out.println(buffer);
}
audioRecord.stop();
audioRecord.release();
}
Log.e("recording", "stopeed");
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
I get following error every time i try to test the program
06-04 00:18:17.222: E/AudioRecord-Java(488): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.
Upvotes: 5
Views: 8602
Reputation: 625
A may be this can be answer?:
Params:
audioSource – the recording source. See MediaRecorder.AudioSource for the recording source definitions.
sampleRateInHz – the sample rate expressed in Hertz. 44100Hz is currently the only rate that is guaranteed to work on all devices, but other rates such as 22050, 16000, and 11025 may work on some devices.
AudioFormat.SAMPLE_RATE_UNSPECIFIED means to use a route-dependent value which is usually the sample rate of the source. getSampleRate() can be used to retrieve the actual sample rate chosen.
channelConfig – describes the configuration of the audio channels. See AudioFormat.CHANNEL_IN_MONO and AudioFormat.CHANNEL_IN_STEREO. AudioFormat.CHANNEL_IN_MONO is guaranteed to work on all devices.
audioFormat – the format in which the audio data is to be returned. See AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT, and AudioFormat.ENCODING_PCM_FLOAT.
bufferSizeInBytes – the total size (in bytes) of the buffer where audio data is written to during the recording. New audio data can be read from this buffer in smaller chunks than this size. See getMinBufferSize(int, int, int) to determine the minimum required buffer size for the successful creation of an AudioRecord instance. Using values smaller than getMinBufferSize() will result in an initialization failure.
Upvotes: 1
Reputation: 8772
This exception is also raised if
Upvotes: 7
Reputation: 130
From what I understand, CHANNEL_CONFIGURATION_MONO is depreciated and you should use instead CHANNEL_IN_MONO when reading into the buffer. I had a similar problem with instantiating the AudioRecord object and this turned out to be the solution for me.
Upvotes: 1