1

I have a problem regarding the initialization of AudioRecord on Android. My application simply consists of listening to your voice from the speaker while speaking in real time.

But I have problems from the start. My initialization code is as follows:

 int Rate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_MUSIC);


 int MinBufferAM = AudioRecord.getMinBufferSize(Rate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
 AudioRecord AR = new AudioRecord(MediaRecorder.AudioSource.VOICE_COMMUNICATION, Rate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, MinBufferAM);


 int MinBufferAT = AudioTrack.getMinBufferSize(Rate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
 AudioTrack AT = new AudioTrack(AudioManager.MODE_IN_COMMUNICATION, Rate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, MinBufferAT, AudioTrack.MODE_STREAM);

The error that appears when running the app is this:

E/AudioRecord: Could not get audio input for session 89, record source 7, sample rate 44100, format 0x1, channel mask 0x10, flags 0

E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -22.

Error code -20 when initializing native AudioRecord object.

As you can see it is a very simple code, taken from both the official documentation of Android and its variants, here on stackoverflow. But nobody works for me. Unlike the other error codes I've seen here, in addition to the number -20 I also see the number -22. What does it mean? I've tested the code on two computers and on a real device. Always the same mistake.

Obviously I have set permissions (RECORD_AUDIO) and activated on the emulator / devices but nothing. That's why I took the liberty ask the question, as the solution they give to everyone is that on permissions. So please, where am I wrong?

I would also like to ask if there is any book explaining how audio is being exploited in Android. So similar to the book "Learning Core Audio" for iOS. Finally there is some site that identifies errors in Android, such as "OSStatus.com" for iOS?

Thank you for your attention.

UPDATE 1

I continued to try but nothing works. I also tried to put non-recommended code lines like System.exit() but it does not work. I see that other users have put my problem with the error -22, but no answer. I tried other ways to implement AudioRecord, but nothing.

Again, the problem is not the first time that AudioRecord is initialized and then released at the end, but at the second run of the application. I also saw if I changed my project and tried to run another application, always AudioRecord, I would immediately get the error. So it makes me think: is this a problem with my Application? Is this a problem with AndroidStudio? Otherwise what other tests could I make to understand the problem?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Fantastico
  • 31
  • 7
  • I too am getting same error on Android 7. Works on android 6. Were you able to fix it? – Akshay Shah May 15 '17 at 16:25
  • Hello. I solved it only in half, so I did not write the solution. The partial solution is to use release() when you do not need to use AudioRecord. In my example, it would be `AR.release();` This works, but if it stops the simulator, or the app during execution, the error starts again when it starts. On this, I did not manage to solve the problem. It's as if the AudioRecord is always running and does not delete when I no longer need or stop running the app. – Fantastico May 15 '17 at 18:47
  • I don't know the solution, but I see many posts about error -20... Such as https://stackoverflow.com/questions/33229468/audiorecord-object-not-initializing-in-a-project – OneCricketeer Jun 08 '17 at 23:58
  • Hi, thank you for having responded to me. I've seen the link and maybe you've taken me on the right track. I was able to test my app on an older device with Android 4.1 and it worked smoothly. This leads me to think that it is a version issue, as indicated in the link you provided to me. I tried to put the permissions indicated, even via code and as recommended by the official documentation, but nothing, it does not work for Android 7.1. So what other tests could I do? – Fantastico Jun 09 '17 at 16:57

1 Answers1

0

I think the problem is in your program. I faced with this problem too and I solved it then turned on record permission manually for my application in settings on my device. Therefore the problem consists in wrong setting of permission in program files, not in Android Studio.

FIL_94
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 21 '22 at 01:33
  • Hi, thanks for the reply, even if I'm late, I want to tell you how it went 6 years ago. The solution was simple: I updated Android Studio after I had the problem and it worked. I think it really was a problem with Android Studio as it was tested on two computers with two different operating systems and it gave the same problem, but then updating it the problem no longer occurred. – Fantastico Jul 23 '23 at 22:50