0

当我尝试使用以下代码通过模拟器录制音频时

  AudioRecord recordInstance = new AudioRecord(
    MediaRecorder.AudioSource.MIC, this.getFrequency(), this
      .getChannelConfiguration(), this.getAudioEncoding(),
    bufferSize);

然后我在 logcat 中得到以下异常:

12-16 19:07:31.680: INFO/jdwp(223): Ignoring second debugger -- accepting and dropping
12-16 19:07:31.700: ERROR/AudioHardware(34): Error opening input channel
12-16 19:07:31.720: WARN/AudioHardwareInterface(34): getInputBufferSize bad sampling rate: 11025
12-16 19:07:31.730: ERROR/AudioRecord(294): Recording parameters are not supported: sampleRate 11025, channelCount 1, format 1
12-16 19:07:31.730: ERROR/AudioRecord-JNI(294): Error creating AudioRecord instance: initialization check failed.

12-16 19:07:31.730: ERROR/AudioRecord-Java(294): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.
12-16 19:07:31.730: WARN/dalvikvm(294): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
12-16 19:07:31.770: ERROR/AndroidRuntime(294): FATAL EXCEPTION: Thread-8
12-16 19:07:31.770: ERROR/AndroidRuntime(294): java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
12-16 19:07:31.770: ERROR/AndroidRuntime(294):     at android.media.AudioRecord.startRecording(AudioRecord.java:495)
12-16 19:07:31.770: ERROR/AndroidRuntime(294):     at com.prospeak.Recorder.run(Recorder.java:84)
12-16 19:07:31.770: ERROR/AndroidRuntime(294):     at java.lang.Thread.run(Thread.java:1096)

你能弄清楚这段代码有什么问题吗?

4

7 回答 7

3

您的采样率错误,请尝试 8000Hz。这是一个模拟器限制。

于 2010-12-16T13:50:27.863 回答
2

此外,请确保您在 AndroidManifest.xml 中设置了此权限: <uses-permission android:name="android.permission.RECORD_AUDIO" />

于 2014-01-07T16:34:17.417 回答
1

如果您尚未在清单中注册录制音频的权限,那么您将收到错误消息。

于 2010-12-16T14:35:43.430 回答
0

我最近在一个类似的问题上苦苦挣扎。如前所述,您必须循环查看采样率、通道配置、音频格式的所有可能组合,以了解适合给定手机的内容。模拟器 AFAIK,不支持音频输入,并且总是会在 AudioRecord 对象上给出初始化错误。所以你必须在真实设备上测试你的代码。但此外,我发现您可以为 AudioRecord 对象提供的缓冲区大小有一个最大限制。它必须在几个 KB 的某个地方。我尝试将其设置为大约 10 MB,但它一直给出相同的初始化错误。所以要小心。这是循环显示格式和采样率的代码(由 StackEx 某处提供):

 private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
private static short [] aformats = new short[] { AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT };
private static short [] chConfigs = new short[] { AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO };


public AudioRecord findAudioRecord() {
    for (int rate : mSampleRates) {
        for (short audioFormat : aformats) {
            for (short channelConfig : chConfigs) {
                try {
                    Log.d("Log:", "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: "
                            + channelConfig);
                    int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);

                    if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
                        // check if we can instantiate and have a success
                        AudioRecord recorder = new AudioRecord(android.media.MediaRecorder.AudioSource.MIC, rate, channelConfig, audioFormat, java.lang.Math.max(bufferSize,1024*800));

                        if (recorder.getState() == AudioRecord.STATE_INITIALIZED){
                            Chosen_SR = rate;
                            audFormat = audioFormat;
                            chanConfig = channelConfig;
                            return recorder;
                        }

                    }
                } catch (Exception e) {
                    Log.e("Log:", rate + "Exception, keep trying.",e);
                }
            }
        }
    }
    return null;
}
AudioRecorder mRecord = findAudioRecord();

希望有帮助。

于 2013-10-10T13:54:39.303 回答
0

确保检查audiorecord.getState() == initialized.

该链接帖子中的示例代码:

https://stackoverflow.com/questions/tagged/sample-rate+android

于 2011-08-11T14:55:20.743 回答
0

错误是:

getInputBufferSize bad sampling rate: 11025

您需要不同的采样率。您可以遍历潜在的采样率,直到没有异常为止。

至于尝试从模拟器录制,它在 Android 开发者网站上说这是不可能的,因此最好从手机进行测试或使用预先录制的样本。

于 2010-12-20T08:58:27.207 回答
0

伙计们的录音只能用,

    AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
            8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
            AudioFormat.ENCODING_PCM_16BIT, 500000);
    recorder.startRecording();

我搜索了很长时间,最后发现,但它只会产生噪音,尝试获得高质量的音频

于 2011-08-24T02:55:09.617 回答