0

I am trying to get AudioRecord instance but I get errors:

Could not get audio input for record source 1

Error creating AudioRecord instance: initialization check failed.

Error code -20 when initializing native AudioRecord object.

Here is my sample code:

public class AudioCapturer {
    AudioRecord recorder;
    private int channelConfig = AudioFormat.CHANNEL_IN_STEREO;
    private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
    private static final int AUDIO_SAMPLE_RATE = 8000;
    private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
    int bufferSize = AudioRecord.getMinBufferSize(mSampleRates[3], channelConfig, audioFormat);


    protected void Start() {
        new Thread(new Runnable() {
                public void run() {
                    recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, mSampleRates[3], channelConfig, audioFormat, bufferSize);
                    Log.v("RECORDER","" +recorder.getState());
                    recorder.startRecording();

                    while(recorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING)
                    {
                        byte [] buffer = new byte[bufferSize];
                        Log.v("READ RESULT","" + recorder.read(buffer, 0, buffer.length));
                    }
                }
            }).start();
    }
}

I've added permissions but result is still the same.
My mobile phone API version is 18.

Ivar
  • 6,138
  • 12
  • 49
  • 61
Sebastianor
  • 196
  • 10

1 Answers1

0

Most probably you can solve the issue changing

private int channelConfig = AudioFormat.CHANNEL_IN_STEREO;

to

private int channelConfig = AudioFormat.CHANNEL_IN_MONO;

Better solution would be to loop through all possible parameter values combinations to select working combination. See AudioRecord object not initializing

Anton Malyshev
  • 8,686
  • 2
  • 27
  • 45