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?