当我尝试使用以下代码通过模拟器录制音频时
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)
你能弄清楚这段代码中有什么问题吗?
答案 0 :(得分:3)
您的采样率是错误的,请尝试8000Hz。这是一个模拟器限制。
答案 1 :(得分:2)
另外,请确保您在AndroidManifest.xml中设置了此权限:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
答案 2 :(得分:1)
如果您尚未在清单中注册录制音频的权限,则会收到错误。
答案 3 :(得分:0)
错误是:
getInputBufferSize bad sampling rate: 11025
您需要不同的采样率。您可以循环查看潜在的采样率,直到您没有异常。
至于尝试从模拟器录制时,它在Android开发者网站上说这是不可能的,所以最好是从手机测试或使用预先录制的样本。
答案 4 :(得分:0)
请务必检查audiorecord.getState() == initialized
。
该帖子中的示例代码:
https://stackoverflow.com/questions/tagged/sample-rate+android
答案 5 :(得分:0)
家伙录音只能继续工作,
AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
8000, AudioFormat.CHANNEL_CONFIGURATION_MONO,
AudioFormat.ENCODING_PCM_16BIT, 500000);
recorder.startRecording();
我搜索了很长时间并且最后发现了,但它只产生噪音,试图获得高质量的音频
答案 6 :(得分: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();
希望有所帮助。