Android 中录制出现异常

发布于 2024-10-07 22:46:32 字数 1583 浏览 1 评论 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)

你能找出这段代码出了什么问题吗?

When I am trying to record an audio through the emulator using following code

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

Then I am getting following exceptions in 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)

Can you figure out what's wrong in this code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

猫弦 2024-10-14 22:46:32

你的采样率不对,试试8000Hz。这是模拟器的限制。

your sample rate is wrong, try 8000Hz. It is an emulator limitation.

牵你的手,一向走下去 2024-10-14 22:46:32

另外,请确保您在 AndroidManifest.xml 中设置了此权限:

Also, make sure that you have this permission set in your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECORD_AUDIO" />

ま昔日黯然 2024-10-14 22:46:32

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

If you haven't registered the permission to record audio in your manifest then you will get an error.

葬花如无物 2024-10-14 22:46:32

错误是:

getInputBufferSize bad sampling rate: 11025

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

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

The error is:

getInputBufferSize bad sampling rate: 11025

You need a different sample rate. You could loop through potential sample rates until you don't have an Exception.

As far as trying to record from the Emulator, it says on the Android Developer site that it's not possible so it would probably be best to test from a phone or to use a pre-recorded sample.

风向决定发型 2024-10-14 22:46:32

伙计们,音频记录只能工作,

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

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

guys audio record only work on,

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

i search it for long time and last found that, but it only produces noise, try to get good quality audio

月光色 2024-10-14 22:46:32

确保检查 audiorecord.getState() == 已初始化

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

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

Make sure that you check that the audiorecord.getState() == initialized.

Sample code in that linked post:

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

柒七 2024-10-14 22:46:32

我最近遇到了类似的问题。如前所述,您必须循环浏览采样率、通道配置、音频格式的所有可能组合,才能知道什么适合给定的手机。据我所知,模拟器不支持音频输入,并且始终会在 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();

希望有帮助。

I struggled with a similar issue recently. As said earlier you have to cycle through all possible combinations of Sample rates, Channel configurations, Audio Formats to know what fits for a given phone. The emulator AFAIK, does not support audio input and will always give an initialization error on AudioRecord objects. So you have to test your code on a real device.
But additionally, I discovered that there is a maximum limit to the buffer size you can give for the AudioRecord object. It must be somewhere in a few KB's. I tried to set it at about 10 MB and it kept giving the same initialization error. So be careful. Here is the code to cycle through the Formats and Sample rates(courtesy somewhere on 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();

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文