我已经启动了Media Recorder,设置了音频源,输出格式,输出文件和音频编码器。之后,我使用了mediaRecorder.prepare()方法,并进行了一次尝试,然后启动了它。我还在清单中添加了WRITE_EXTERNAL_STORAGE和RECORD_AUDIO的权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
检查权限会返回true,因此创建活动后便会授予权限,所以我知道这不是问题
pathSave = Environment.getExternalStorageDirectory().getAbsolutePath();
pathSave += "/" + UUID.randomUUID() + ".3gp";
Log.d("path", pathSave);
setupMediaRecorder();
try {
mediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaRecorder.start();
private void setupMediaRecorder() {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(pathSave);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
}
这是错误日志:
09-17 08:46:18.813 4178-4178/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: maginate.net.thebestrapper, PID: 4178
java.lang.IllegalStateException
at android.media.MediaRecorder._start(Native Method)
at android.media.MediaRecorder.start(MediaRecorder.java:946)
at maginate.net.thebestrapper.session.RecordActivity$1.onClick(RecordActivity.java:185)
at android.view.View.performClick(View.java:5697)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)