Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AudioFlinger could not create record track, status: -1 , Need help to ifx

public class AudioRecorderActivity extends Activity {

  private static final int RECORDER_SAMPLERATE        = 8000;
  private static final int RECORDER_CHANNELS          = AudioFormat.CHANNEL_IN_MONO;
  private static final int RECORDER_AUDIO_ENCODING    = AudioFormat.ENCODING_PCM_16BIT;
  private AudioRecord recorder                        = null;
  private static final String TAG                     = "AudioRecorderActivity";
  short[][]   buffers                                 = new short[256][160];
  int         ix                                      = 0;
  private boolean     stopped                         = false;

  private void startRecording() {

    android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
    try {
      int N = AudioRecord.getMinBufferSize (  
                          RECORDER_SAMPLERATE, 
                          RECORDER_CHANNELS, 
                          RECORDER_AUDIO_ENCODING) * 20;

      recorder = new  AudioRecord(AudioSource.MIC,
                    RECORDER_SAMPLERATE, 
                    RECORDER_CHANNELS,
                    RECORDER_AUDIO_ENCODING,N );        

      recorder.startRecording();

      while(!stopped) {
        short[] buffer = buffers[ix++ % buffers.length];      
        N = recorder.read(buffer,0,buffer.length);
      }

    } 
    catch(Throwable x) { 
      Log.v(TAG,"Error reading voice audio",x);
      x.printStackTrace();
    } 
    finally { 
      stopped = true;
      stopRecording();
    }    
  }  
}

Question : Though the code snippet is based on an exaple from StackOverflow, it is not working Please let me know what could be the mistake ?

Here is the Error Message

12-20 03:44:32.271: E/AudioRecord(224): AudioFlinger could not create record track, status: -1
12-20 03:44:32.271: E/AudioRecord-JNI(224): Error creating AudioRecord instance: initialization check failed.
12-20 03:44:32.271: E/AudioRecord-Java(224): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.
like image 235
user3120896 Avatar asked Dec 19 '13 22:12

user3120896


2 Answers

Add record_audio permission to AndroidManifest.xml

<uses-permission android:name="android.permission.RECORD_AUDIO" />
like image 159
Olexij Moroz Avatar answered Oct 27 '22 18:10

Olexij Moroz


On recent Android versions, it seems that repushing the apk from Android Studio after changing the permissions in the manifest doesn't actually change the permission in the app settings. This is why the above answer worked for me. I had built an application without the RECORD_AUDIO permission in my manifest, and seen the permission error in my log (along with the errors in the OP). I added the permission to my manifest and reran the application on device, the permission error in the log was no longer there but I still got the errors in the OP. I went to my System, Applications, Application Manager -> MyApp -> Permissions, and saw that Record Audio was listed but still turned off. Manually turning that on fixed it.

I imagine (but haven't confirmed) that if I built the correct permission into my manifest in the first place, this would have just worked for me. Alternatively, uninstalling my app from the device, and then reinstalling, might fix too, but I haven't tried that either.

like image 37
Dave Hooper Avatar answered Oct 27 '22 16:10

Dave Hooper