Android: AudioRecord Error code -20 when initializing AudioRecord native object

Android: I want to read buffers from a microphone so that I can execute a process on it, Below 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 the following error every time I try to test a program

06-04 00: 18: 17.222: E / AudioRecord-Java (488): [android.media.AudioRecord] Error code -20 when initializing its own AudioRecord object.

+4
source share
2 answers

From what I understand, CHANNEL_CONFIGURATION_MONO is depreciating, and you should use CHANNEL_IN_MONO instead when reading to the clipboard. I had a similar problem with creating an AudioRecord object, and this turned out to be a solution for me.

0
source

This exception also occurs if

  • audio recording is already in progress or
  • record unavailable or
  • The application does not have proper permission: the application does not have write permission, etc.
+6
source

Source: https://habr.com/ru/post/1415845/


All Articles