【问题标题】:<AudioRecord> "Could not get audio input for record source 1"<AudioRecord> "无法获得记录源 1 的音频输入"
【发布时间】:2011-06-15 23:58:11
【问题描述】:

我在为 Android 初始化 AudioRecord 时遇到了这个问题。我在网上搜索了很长时间,没有成功。

对于手机,我在 SDK 版本 7 上使用三星 GalaxyS。对于 AudioRecord 初始化,我使用 8000 作为采样率,单声道配置为单声道,音频格式为 16 位,并根据log,minBufferSize设置为4160。我在manifest中添加了AUDIO_RECORD权限。

我的初始化代码如下:

...
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];
}
...

但是,对象 (this.recorder) 未能初始化。以下来自使用 DDMS 的日志:

AUDIO-BUFFER-SIZE(3253): 4160
AudioRecord(3253): set():sampleRate 8000,通道 16,frameCount 2080
AudioPolicyManager(2175): getInput() inputSource 1, samplingRate 8000, format 1, channels 10, acoustics 0
AudioFlinger(2175): openInput() openInputStream 返回输入0x0,SamplingRate 8000,格式 1,通道 10,声学 0,状态 -17
AudioRecord(3253): 无法获取记录源 1 的音频输入
AudioRecord-JNI (3253): 创建 AudioRecord 实例时出错:初始化检查失败。
AudioRecord-Java(3253): [ android.media.AudioRecord ] 错误代码 -20 初始化原生 AudioRecord 对象时。

有什么帮助吗?非常感谢!

【问题讨论】:

    标签: android audio-recording android-sdk-2.1 android-audiorecord


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 请注意我上面的回答,我的手机,也是三星 Galaxy S,有一个原生层,对调用 AudioRecord.release() 很挑剔。
      • Galaxy S 也是如此。关于如何确保我不会得到这个的任何想法? O.o
      • 我得到了同样的结果,当我重新启动它开始工作。有什么办法让问题不再重复
      【解决方案3】:

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

      【讨论】:

        【解决方案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");
                }
            }
        }
        

        我跳过这个帮助。

        迪克万

        【讨论】:

          【解决方案5】:

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

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

          samsung 有 bug 追踪器吗?

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              【解决方案7】:

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

              【讨论】:

                猜你喜欢
                • 2018-05-14
                • 2011-06-25
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-08-18
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多