2

I used following Tutorial to make a sound recording app:

When I start it on my test device (LG P970 Optimus Black) I get an error code:

 Error creating AudioRecord instance: initialization check failed.
 Error code -20 when initializing native AudioRecord object.

I found some "solutions" like adding to the Manifest the RECORD_AUDIO permission and use the .release() method. But all this stuff was allready in the source code.

Any other solutions for this problem? Could it be the device?

user2043332
  • 395
  • 4
  • 17
  • http://stackoverflow.com/questions/4843739/audiorecord-object-not-initializing – Jay Mayu Mar 11 '13 at 09:25
  • Thanks, but I can't comment the solution of @Dustin in this topic. I tried this solution and now I'am getting the error getMinBuferSize() Invalid audio format – user2043332 Mar 11 '13 at 11:09
  • You can't use 8-bit with getMinBufferSize() last I checked. Use 16-bit instead – alrikai Oct 11 '13 at 23:16

4 Answers4

2

I have solved this error Like this.becuse we can call AudioRecors object like this.

private AudioRecord findAudioRecord() {
        // TODO Auto-generated method stub
           for (int rate : mSampleRates) {

               for (short audioFormat : new short[] { AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT }) {

                   for (short channelConfig : new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO }) {

                       try {

                           Log.d("AudiopRecording", "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: "

                                   + channelConfig);

                           int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);

                           if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {

                               // check if we can instantiate and have a success

                               AudioRecord recorder = new AudioRecord(AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);

                               if (recorder.getState() == AudioRecord.STATE_INITIALIZED)

                                   return recorder;

                           }

                       } catch (Exception e) {

                           Log.e("AudiopRecording", rate + "Exception, keep trying.",e);

                       }

                   }

               }

           }


        return null;
    }
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
2

change audio source in AudioRecord as Default..... i was able to solve this problem by using

MediaRecorder.AudioSource.DEFAULT 
Hash_S
  • 79
  • 4
  • 18
1

This error you get because of buffer size you specify in AudioRecord constructor.

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.

That why always use AudioRecord like this:

     try {

       int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);



       AudioRecord recorder = new AudioRecord(AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);

                       if (recorder.getState() == AudioRecord.STATE_INITIALIZED){

                               return recorder;

                       }

       } catch (Exception e) {

       Log.i("AudioRecording", "Error in Audio Record");

       }
Bhagirathsinh Gohil
  • 673
  • 1
  • 9
  • 25
1

This might be because your android device is blocking your app permission. Go To Your Android Device's Setting>Permissions>Your_App and Allow Record Audio. This Solved problem for me though

Naung9
  • 209
  • 1
  • 8