5

I am trying to take small recordings to find the Sound Pressure Level from a service but Android wont give me access to the hardware. I get the following errors in Logcat:

alt text

The error comes from the following code:

AudioRecord recordInstance = null;

    // We're important...
    android.os.Process
            .setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

    short bufferSize = 4096;// 2048;


    recordInstance = new AudioRecord(MediaRecorder.AudioSource.MIC, this //line 167
            .getFrequency(), this.getChannelConfiguration(), this
            .getAudioEncoding(), bufferSize); //object not created

    tempBuffer = new short[bufferSize];
    recordInstance.startRecording();

What happens is that recordInstance is never properly created and so when it gets to the end and calls recordInstance.startRecording(), recordInstance is still null. Android rejects my programs request at the definition. Does anyone know what those errno's indicate? I couldn't find a list online.

AudioRecord Docs

Thanks

FuegoFingers
  • 2,181
  • 6
  • 27
  • 36

1 Answers1

19

Check three things:

  1. Your permissions (you need to give permission for RECORD_AUDIO)

  2. Have you run this once and not called recordInstance.release()? If so you may have tied up audio resources and it will likely not work until you restart the phone. I have found that in my experience anyway.

3.The buffer size. There is a static method AudioRecord.getMinBufferSize()

chaimp
  • 16,897
  • 16
  • 53
  • 86
  • 3
    It was the recordInstance.release(). Thanks – FuegoFingers Dec 23 '10 at 08:06
  • The hard part is when working on things and testing, sometimes the program crashes and how to call recordInstance.release() then? In fact, I'm going to make a new question about that right now. – chaimp Dec 24 '10 at 06:19
  • Making sure all 3 conditions are met fixed the issue for me. Nice one(+1). although hacky, wouldn't an universal/uncaught exception handler do the trick ? – George Profenza Aug 08 '13 at 10:02