尝试为媒体记录器设置编码器时发生 IllegalStateException
IllegalStateException was happen when try to set Encoder for Media Recorder
目前我正在与 Custom Camera for Recording video
合作。
我已经completed Take Picture from Camera feature
正确了。
我也在尝试making Recording Video from Camera feature work
。
实际上,如果我不尝试为视频设置 Encoder
,它就成功了。
但是我想自定义录制视频功能,所以我已经为 Media Recorder
设置了 Encoder
,并且总是得到 IllegalStateException
。
所以我想要的是:Can set encoder, both audio encoder and video encoder for video
完全没有发生异常。
知道怎么做的人,
请告诉我,
真的谢谢你,
下面是代码。
private boolean prepareVideoRecorder(int mode){
// Should release before use new Preview for Recording Video mode
CustomCamera.releaseCamera();
// Initialize camera
CustomCamera.mCamera = CustomCamera.getCameraInstance(mode);
// Set orientation display
CustomCamera.setCameraDisplayOrientation(getActivity(), mode);
// Should release before use new Preview for Recording Video mode
CustomCamera.releaseMediaRecorder();
CustomCamera.mMediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
CustomCamera.mCamera.unlock();
CustomCamera.mMediaRecorder.setCamera(CustomCamera.mCamera);
// Step 2: Set sources
CustomCamera.mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
CustomCamera.mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// THESE LINES GET CRASH
CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
CustomCamera.mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
// todo Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
CustomCamera.mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
// Step 4: Set output file
CustomCamera.mMediaRecorder.setOutputFile(getOutputMediaFile(MediaType.VIDEO).toString());
// Step 5: Set the preview output
CustomCamera.mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
try {
CustomCamera.mMediaRecorder.prepare();
CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
} catch (IllegalStateException e) {
e.printStackTrace();
CustomCamera.releaseMediaRecorder();
return false;
} catch (IOException e) {
e.printStackTrace();
CustomCamera.releaseMediaRecorder();
return false;
}
return true;
}
p/s :
虽然我设置了这些行
CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
CustomCamera.mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
在方法 setProfile
(或 setOutputFile
)之前或之后仍然会出现此异常。
这里是 thrown exception
:
> I/MediaRecorderJNI﹕ prepare: surface=0x1cf9610 (identity=373)
E/MediaRecorder﹕ audio encoder has already been set
java.lang.IllegalStateException
at android.media.MediaRecorder.setAudioEncoder(Native Method)
at ui.fragment.custom.camera.CameraPreviewFragment.prepareVideoRecorder(CameraPreviewFragment.java:469)
at ui.fragment.custom.camera.CameraPreviewFragment.onClick(CameraPreviewFragment.java:203)
at android.view.View.performClick(View.java:3574)
at android.view.View$PerformClick.run(View.java:14293)
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:4448)
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:823)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
at dalvik.system.NativeStart.main(Native Method)
解决方案
在setProfile()
方法后设置以下代码
CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
CustomCamera.mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
并删除 try-catch
异常中的 .setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
行。它工作正常。
从 try catch 中删除 CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
行,因为您在此处尝试再次设置 AudioEncoder,因此出现异常。
编辑
在setProfile()
方法后设置Encoder
,是正确的。
目前我正在与 Custom Camera for Recording video
合作。
我已经completed Take Picture from Camera feature
正确了。
我也在尝试making Recording Video from Camera feature work
。
实际上,如果我不尝试为视频设置 Encoder
,它就成功了。
但是我想自定义录制视频功能,所以我已经为 Media Recorder
设置了 Encoder
,并且总是得到 IllegalStateException
。
所以我想要的是:Can set encoder, both audio encoder and video encoder for video
完全没有发生异常。
知道怎么做的人,
请告诉我,
真的谢谢你,
下面是代码。
private boolean prepareVideoRecorder(int mode){
// Should release before use new Preview for Recording Video mode
CustomCamera.releaseCamera();
// Initialize camera
CustomCamera.mCamera = CustomCamera.getCameraInstance(mode);
// Set orientation display
CustomCamera.setCameraDisplayOrientation(getActivity(), mode);
// Should release before use new Preview for Recording Video mode
CustomCamera.releaseMediaRecorder();
CustomCamera.mMediaRecorder = new MediaRecorder();
// Step 1: Unlock and set camera to MediaRecorder
CustomCamera.mCamera.unlock();
CustomCamera.mMediaRecorder.setCamera(CustomCamera.mCamera);
// Step 2: Set sources
CustomCamera.mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
CustomCamera.mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// THESE LINES GET CRASH
CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
CustomCamera.mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
// todo Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
CustomCamera.mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
// Step 4: Set output file
CustomCamera.mMediaRecorder.setOutputFile(getOutputMediaFile(MediaType.VIDEO).toString());
// Step 5: Set the preview output
CustomCamera.mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder().getSurface());
// Step 6: Prepare configured MediaRecorder
try {
CustomCamera.mMediaRecorder.prepare();
CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
} catch (IllegalStateException e) {
e.printStackTrace();
CustomCamera.releaseMediaRecorder();
return false;
} catch (IOException e) {
e.printStackTrace();
CustomCamera.releaseMediaRecorder();
return false;
}
return true;
}
p/s :
虽然我设置了这些行
CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
CustomCamera.mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
在方法 setProfile
(或 setOutputFile
)之前或之后仍然会出现此异常。
这里是 thrown exception
:
> I/MediaRecorderJNI﹕ prepare: surface=0x1cf9610 (identity=373)
E/MediaRecorder﹕ audio encoder has already been set
java.lang.IllegalStateException
at android.media.MediaRecorder.setAudioEncoder(Native Method)
at ui.fragment.custom.camera.CameraPreviewFragment.prepareVideoRecorder(CameraPreviewFragment.java:469)
at ui.fragment.custom.camera.CameraPreviewFragment.onClick(CameraPreviewFragment.java:203)
at android.view.View.performClick(View.java:3574)
at android.view.View$PerformClick.run(View.java:14293)
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:4448)
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:823)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
at dalvik.system.NativeStart.main(Native Method)
解决方案
在setProfile()
方法后设置以下代码
CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
CustomCamera.mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
并删除 try-catch
异常中的 .setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
行。它工作正常。
从 try catch 中删除 CustomCamera.mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
行,因为您在此处尝试再次设置 AudioEncoder,因此出现异常。
编辑
在setProfile()
方法后设置Encoder
,是正确的。