Cannot access AudioRecorder

我是研究僧i 提交于 2019-12-07 04:45:18

问题


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:

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


回答1:


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()



来源:https://stackoverflow.com/questions/4342482/cannot-access-audiorecorder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!