Android 中录制出现异常
当我尝试使用以下代码通过模拟器录制音频时
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
你的采样率不对,试试8000Hz。这是模拟器的限制。
your sample rate is wrong, try 8000Hz. It is an emulator limitation.
另外,请确保您在 AndroidManifest.xml 中设置了此权限:
Also, make sure that you have this permission set in your AndroidManifest.xml:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
如果您尚未在清单中注册录制音频的权限,则会收到错误消息。
If you haven't registered the permission to record audio in your manifest then you will get an error.
错误是:
您需要不同的采样率。您可以循环遍历潜在的采样率,直到没有异常为止。
至于尝试从模拟器进行录制,Android 开发者网站上表示这是不可能的,因此最好从手机进行测试或使用预先录制的示例。
The error is:
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.
伙计们,音频记录只能工作,
我搜索了很长时间,最后发现了,但它只会产生噪音,尝试获得高质量的音频
guys audio record only work on,
i search it for long time and last found that, but it only produces noise, try to get good quality audio
确保检查
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
我最近遇到了类似的问题。如前所述,您必须循环浏览采样率、通道配置、音频格式的所有可能组合,才能知道什么适合给定的手机。据我所知,模拟器不支持音频输入,并且始终会在 AudioRecord 对象上给出初始化错误。所以你必须在真实设备上测试你的代码。
但此外,我发现可以为 AudioRecord 对象提供的缓冲区大小有最大限制。它一定在几 KB 的某个地方。我尝试将其设置为大约 10 MB,但它一直给出相同的初始化错误。所以要小心。以下是循环浏览格式和采样率的代码(由 StackEx 上的某处提供):
希望有帮助。
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):
Hope that helps.