设置MediaRecorder时为什么显示错误IllegalStateException? [英] Why show error IllegalStateException when setting MediaRecorder?
问题描述
我的代码设置为MediaRecorder,它在行设置质量时显示错误
My code setting MediaRecorder, it show error at row set Quality
mMediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.stopPreview();
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setProfile(CamcorderProfile .get(CamcorderProfile.QUALITY_HIGH));
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
// Step 4: Set output file
mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
try {
mMediaRecorder.prepare();
Log.d("DEBUG", "IllegalStateException preparing MediaRecorder: "
+ e.getMessage());
releaseMediaRecorder();
return false;
} catch (IOException e) {
Log.d("DEBUG",
"IOException preparing MediaRecorder: " + e.getMessage());
releaseMediaRecorder();
return false;
}
例如:
java.lang.IllegalStateException
Stacktrace:
Stacktrace:
java.lang.IllegalStateException
at android.media.MediaRecorder.setOutputFormat(Native Method)
at android.media.MediaRecorder.setProfile(MediaRecorder.java:366)
at jp.osaka.E028.prepareVideoRecorder(E028.java:1441)
at jp.osaka.E028.access$16(E028.java:1403)
at jp.osaka.E028$6.onClick(E028.java:344)
at android.view.View.performClick(View.java:3517)
at android.view.View$PerformClick.run(View.java:14155)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4503)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
at dalvik.system.NativeStart.main(Native Method)
为什么在设置MediaRecorder时显示错误IllegalStateException?
Why show error IllegalStateException when setting MediaRecorder?
推荐答案
实际上,您执行两次mMediaRecorder.setOutputFormat()
:一次明确地执行一次,然后mMediaRecorder.setProfile()
尝试再次执行该操作,如在堆栈跟踪中所见.
Actually you do mMediaRecorder.setOutputFormat()
twice: One time explicitly and afterwards mMediaRecorder.setProfile()
tries to do it again as you can see in your stacktrace.
Android Media Recorder对此类事情的鲁棒性很低.
The Android Media Recorder has a very low robustness for things like that.
因此,删除显示以下内容的行
So remove the line that says
mMediaRecorder.setOutputFormat();
,错误应消失.顺便说一句.删除
and the error should go away. And btw. remove
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
这也是mMediaRecorder.setProfile()
已经完成的工作.
which is what mMediaRecorder.setProfile()
has already done as well.
这篇关于设置MediaRecorder时为什么显示错误IllegalStateException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!