Android: AudioRecord Error code -20 when initializing native AudioRecord object

吃可爱长大的小学妹 提交于 2019-12-10 13:49:49

问题


Android: I want to read buffers from mic so that i can perform process on it, Following is my code

int sampleRateInHz = 8000;// 44100, 22050 and 11025
        int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
        int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

        //int bufferSize =11025 + 
        int bufferSize = AudioRecord.getMinBufferSize(sampleRateInHz,channelConfig, audioFormat);


        short[] buffer = new short[bufferSize];

        AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz,channelConfig, audioFormat, bufferSize);

        if(audioRecord.getState()== AudioRecord.STATE_INITIALIZED){
            audioRecord.startRecording();
            Log.e("recording", "before");


            boolean flag = true;
            while (flag) {
                int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
                System.out.println(buffer);
            }

            audioRecord.stop();
            audioRecord.release();
        }
        Log.e("recording", "stopeed");


<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>

I get following error every time i try to test the program

06-04 00:18:17.222: E/AudioRecord-Java(488): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.


回答1:


From what I understand, CHANNEL_CONFIGURATION_MONO is depreciated and you should use instead CHANNEL_IN_MONO when reading into the buffer. I had a similar problem with instantiating the AudioRecord object and this turned out to be the solution for me.




回答2:


This exception is also raised if

  1. audio recording is already in progress or
  2. recording is not available or
  3. App does not have proper permission ex: App does not have record permission etc


来源:https://stackoverflow.com/questions/10872751/android-audiorecord-error-code-20-when-initializing-native-audiorecord-object

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