Why does Android AudioRecord initialization fail?

user2043332 picture user2043332 · Mar 11, 2013 · Viewed 7.5k times · Source

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?

Answer

user3114805 picture user3114805 · Oct 21, 2013

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;
    }