简体   繁体   English

Android:AudioRecord初始化本机AudioRecord对象时的错误代码-20

[英]Android: AudioRecord Error code -20 when initializing native AudioRecord object

Android: I want to read buffers from mic so that i can perform process on it, Following is my code Android:我想从麦克风中读取缓冲区,以便我可以对其执行处理,以下是我的代码

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. 06-04 00:18:17.222:E / AudioRecord-Java(488):[android.media.AudioRecord]初始化本机AudioRecord对象时的错误代码-20。

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 应用程序没有适当的权限ex:App没有记录权限等

From what I understand, CHANNEL_CONFIGURATION_MONO is depreciated and you should use instead CHANNEL_IN_MONO when reading into the buffer. 据我所知,CHANNEL_CONFIGURATION_MONO已弃用,在读入缓冲区时应使用CHANNEL_IN_MONO。 I had a similar problem with instantiating the AudioRecord object and this turned out to be the solution for me. 我在实例化AudioRecord对象时遇到了类似的问题,这对我来说是个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM