MediaRecorder IOException:准备失败 [英] MediaRecorder IOException: prepare failed
本文介绍了MediaRecorder IOException:准备失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用MediaRecorder录制语音,我的代码是:
I want to use MediaRecorder to record voice, my code is:
public void record(View v) {
Log.d(TAG, "record");
this.mediaRecorder.setAudioChannels(1);
this.mediaRecorder.setAudioSamplingRate(44100);
this.mediaRecorder.setAudioEncodingBitRate(64000);
this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
this.mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
this.mediaRecorder.setOutputFile(this.file.getAbsolutePath());
this.mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
try {
this.mediaRecorder.prepare();
this.mediaRecorder.start();
// update the buttons
this.setButtonsEnabled(false, true, false);
} catch (IOException e) {
Log.e(TAG, "Failed to record()", e);
}
}
或
public void record(View v) {
Log.d(TAG, "record");
this.mediaRecorder = new MediaRecorder();
this.mediaRecorder.setAudioChannels(1);
this.mediaRecorder.setAudioSamplingRate(8000);
this.mediaRecorder.setAudioEncodingBitRate(16);
this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
this.mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
this.mediaRecorder.setOutputFile(this.file.getAbsolutePath());
this.mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
this.mediaRecorder.prepare();
this.mediaRecorder.start();
// update the buttons
this.setButtonsEnabled(false, true, false);
} catch (IOException e) {
Log.e(TAG, "Failed to record()", e);
}
}
在三星上一切正常,但在戴尔上,两种方法均不成功
On a Samsung all is OK, but on a Dell two methods do not succeed
这是logcat:
02-01 13:56:51.094: E/AudioRecorderDemoActivity(1397): Failed to record()
02-01 13:56:51.094: E/AudioRecorderDemoActivity(1397): java.io.IOException: prepare failed.
02-01 13:56:51.094: E/AudioRecorderDemoActivity(1397): at android.media.MediaRecorder._prepare(Native Method)
02-01 13:56:51.094: E/AudioRecorderDemoActivity(1397): at android.media.MediaRecorder.prepare(MediaRecorder.java:524)
02-01 13:56:51.094: E/AudioRecorderDemoActivity(1397): at com.marakana.android.audiorecorderdemo.AudioRecorderDemoActivity.record(AudioRecorderDemoActivity.java:69)
02-01 14:05:20.074: E/AndroidRuntime(1790): FATAL EXCEPTION: main
02-01 14:05:20.074: E/AndroidRuntime(1790): java.lang.IllegalStateException: Could not execute method of the activity
推荐答案
首先,您的代码看起来还不错.您是否已将必需的权限添加到清单文件?
First at all you code looks fine. Have you added the required permissions to your manifest file?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
如果是,请尝试更换:
this.mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
作者
this.mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
别忘了检查视频文件的路径是否正确.
Don't forget to check if the path of your video file is correct.
这篇关于MediaRecorder IOException:准备失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文