gpt4 book ai didi

android - 为什么在设置 MediaRecorder 时显示错误 IllegalStateException?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:08:57 28 4
gpt4 key购买 nike

我的代码设置 MediaRecorder,它显示行设置质量错误

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

堆栈跟踪:

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?

最佳答案

实际上您执行了两次 mMediaRecorder.setOutputFormat():一次是显式执行,之后 mMediaRecorder.setProfile() 尝试再次执行此操作,如您在堆栈跟踪中所见。

Android Media Recorder 对这类事情的鲁棒性非常低。

所以去掉这行

mMediaRecorder.setOutputFormat();

错误应该消失了。顺便说一句。删除

mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

这也是 mMediaRecorder.setProfile() 已经完成的。

关于android - 为什么在设置 MediaRecorder 时显示错误 IllegalStateException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22877833/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com