本文介绍了Android:初始化本机AudioRecord对象时出现AudioRecord错误代码-20的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android:我想从麦克风读取缓冲区,以便可以对其执行处理,以下是我的代码

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]初始化本机AudioRecord对象时出现错误代码-20。

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

推荐答案

据我了解,CHANNEL_CONFIGURATION_MONO已过时,当您读入缓冲区时,应该改用CHANNEL_IN_MONO。我在实例化AudioRecord对象时遇到了类似的问题,事实证明这是我的解决方案。

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.

这篇关于Android:初始化本机AudioRecord对象时出现AudioRecord错误代码-20的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 23:29