16

I've been having this issue with initializing AudioRecord for Android. I searched for quite a while on the web with no success.

For the phone, I'm using a Samsung GalaxyS on SDK version 7. For the AudioRecord initialization, I'm using 8000 as the sample rate, MONO for channel config, 16bit for audio format, and according to the log, the minBufferSize is set to be 4160. I have added the AUDIO_RECORD permission to the manifest.

My code for the initialization is as follows:

...
private static int SAMPLE_RATE = 8000;
private static int CHANNEL_CONFIG = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private static int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT;
// ??? Both 8Bit and Default are deemed illegal.

public MicVolumeManager() {
    this.bufferSize = AudioRecord.getMinBufferSize(SAMPLE_RATE,
        CHANNEL_CONFIG, AUDIO_FORMAT);
    PhoneDebugger.debug("AUDIO-BUFFER-SIZE", 
        Integer.toString(this.bufferSize));

    this.recorder = new AudioRecord(AudioSource.MIC, SAMPLE_RATE,
        CHANNEL_CONFIG, AUDIO_FORMAT, this.bufferSize);

    this.audioBuffer = new byte[this.bufferSize];
}
...

However, the object (this.recorder) failed to be initialized. The following is from the log using DDMS:

AUDIO-BUFFER-SIZE(3253): 4160
AudioRecord(3253): set(): sampleRate 8000, channels 16, frameCount 2080
AudioPolicyManager(2175): getInput() inputSource 1, samplingRate 8000, format 1, channels 10, acoustics 0
AudioFlinger(2175): openInput() openInputStream returned input 0x0, SamplingRate 8000, Format 1, Channels 10, acoustics 0, status -17
AudioRecord(3253): Could not get audio input for record source 1
AudioRecord-JNI(3253): Error creating AudioRecord instance: initialization check failed.
AudioRecord-Java(3253): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.

Any help please? Many thanks!

4

7 回答 7

16

对我来说,原因是未能为之前的 AudioRecord 实例调用 AudioRecord.release();它占用了 AudioFlinger 中的原生资源,并干扰了后续的 AudioRecord 实例。在 Samsung Fascinate (Galaxy S) Android 2.1 (Eclair) 上看到它;Eclair 或三星的实施可能特别不宽容。

于 2011-07-14T12:55:56.983 回答
9

有同样的错误,直到我重新启动设备。

似乎在我的Galaxy S上,本机 impl 存在问题:多次获取和释放AudioRecorder(在整个手机正常运行期间)会导致该错误。

于 2011-06-07T00:08:45.370 回答
5

使用录音机后,您必须停止并释放它。然后下次再启动录音机就OK了。

于 2012-01-31T09:21:13.053 回答
4

嗨,我在尝试初始化 AudioRecord 对象时遇到了同样的问题,我发现的解决方案是在实际尝试实例化当前 AudioRecord 对象之前测试配置参数。这是我用过的常规:

    /**
 * Scan for the best configuration parameter for AudioRecord object on this device.
 * Constants value are the audio source, the encoding and the number of channels.
 * That means were are actually looking for the fitting sample rate and the minimum
 * buffer size. Once both values have been determined, the corresponding program
 * variable are initialized (audioSource, sampleRate, channelConfig, audioFormat)
 * For each tested sample rate we request the minimum allowed buffer size. Testing the
 * return value let us know if the configuration parameter are good to go on this
 * device or not.
 * 
 * This should be called in at start of the application in onCreate().
 * 
 * */
public void initRecorderParameters(int[] sampleRates){

    for (int i = 0; i < sampleRates.length; ++i){
        try {
            Log.i(TAG, "Indexing "+sampleRates[i]+"Hz Sample Rate");
            int tmpBufferSize = AudioRecord.getMinBufferSize(sampleRates[i], 
                            AudioFormat.CHANNEL_IN_MONO,
                            AudioFormat.ENCODING_PCM_16BIT);

            // Test the minimum allowed buffer size with this configuration on this device.
            if(tmpBufferSize != AudioRecord.ERROR_BAD_VALUE){
                // Seems like we have ourself the optimum AudioRecord parameter for this device.
                AudioRecord tmpRecoder = new AudioRecord(MediaRecorder.AudioSource.MIC, 
                                                        sampleRates[i], 
                                                        AudioFormat.CHANNEL_IN_MONO,
                                                        AudioFormat.ENCODING_PCM_16BIT,
                                                        tmpBufferSize);
                // Test if an AudioRecord instance can be initialized with the given parameters.
                if(tmpRecoder.getState() == AudioRecord.STATE_INITIALIZED){
                    String configResume = "initRecorderParameters(sRates) has found recorder settings supported by the device:"  
                                        + "\nSource   = MICROPHONE"
                                        + "\nsRate    = "+sampleRates[i]+"Hz"
                                        + "\nChannel  = MONO"
                                        + "\nEncoding = 16BIT";
                    Log.i(TAG, configResume);

                    //+++Release temporary recorder resources and leave.
                    tmpRecoder.release();
                    tmpRecoder = null;

                    return;
                }                   
            }else{
                Log.w(TAG, "Incorrect buffer size. Continue sweeping Sampling Rate...");
            }
        } catch (IllegalArgumentException e) {
            Log.e(TAG, "The "+sampleRates[i]+"Hz Sampling Rate is not supported on this device");
        }
    }
}

我跳这个帮助。

迪克万

于 2011-03-06T12:49:51.053 回答
3

如果您没有获得 Record_Audio 许可,那么即使您收到错误,也请关闭您的手机,而不是再次打开,然后运行您的应用程序。

于 2013-04-15T11:16:49.007 回答
3

这也影响了 Sansung Galaxy 选项卡 GT-P1000 和更高版本的 10.1 之一

这个错误很难重现,重新启动是我发现摆脱这种糟糕状态的唯一方法......

samsung 有 bug 追踪器吗?

于 2011-12-30T16:29:37.483 回答
0

我认为音频硬件最多支持10个通道,你设置为输入,所以尝试使用2个通道,看看它是否可以工作。

于 2011-07-07T01:51:57.463 回答