本文介绍了NullPointerException - 如果尝试取消后重新录制视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经集成摄像头,允许用户记录时间的推移videos.When用户在录制视频的取消按钮在那里,它允许用户取消当前recording.My问题是,如果取消后,我尝试重新录制我出现以下情况例外 -
显示java.lang.NullPointerException:尝试从外地'诠释android.media.CamcorderProfile.fileFormat读一空对象引用
在android.media.MediaRecorder.setProfile(MediaRecorder.java:423)
在xyz.fragments.CameraFragment。prepareFastVideoRecorder(CameraFragment.java:448)
在xyz.fragments.CameraFragment.access $ 2300(CameraFragment.java:64)
在xyz.fragments.CameraFragment $ 6.onClick(CameraFragment.java:329)
在android.view.View.performClick(View.java:4756)
在android.view.View $ PerformClick.run(View.java:19761)
在android.os.Handler.handleCallback(Handler.java:739)
在android.os.Handler.dispatchMessage(Handler.java:95)
在android.os.Looper.loop(Looper.java:135)
在android.app.ActivityThread.main(ActivityThread.java:5253)
在java.lang.reflect.Method.invoke(Method.java)
在java.lang.reflect.Method.invoke(Method.java:372)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
这行
<$p$p><$c$c>mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_480P));下面是我的code -
start.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(查看视图){ 如果(prepareFastVideoRecorder()){ mMediaRecorder.start(); }其他{ releaseMediaRecorder();
}
}
}
}
});stop.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(查看视图){
mMediaRecorder.stop(); //停止录制
releaseMediaRecorder(); //释放MediaRecorder对象
mCamera.lock(); //拿相机背部的访问,从
}
}):
私人布尔prepareFastVideoRecorder(){
mMediaRecorder =新MediaRecorder();
尝试{
mCamera.set previewDisplay(NULL);
}赶上(java.io.IOException异常IOE){
Log.e(Bhuvnesh,IOException异常作废preVIEW显示:+ ioe.getMessage());
}
mCamera.stop preVIEW();
//第1步:解锁并设置相机MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
//第2步:设置源
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_480P));
//mMediaRecorder.setCaptureRate(0.1); //捕获帧每10秒
mMediaRecorder.setCaptureRate(5); //捕获帧每1秒
//第4步:设置输出文件的socket
mVideoFile =新的文件(getOutputMediaFile()的toString());
mMediaRecorder.setOutputFile(mVideoFile.getAbsolutePath());
如果(cameratype == 1)
mMediaRecorder.setOrientationHint(90);
否则如果(cameratype == 2)
mMediaRecorder.setOrientationHint(270);
mMediaRecorder.setMaxDuration(60000);
mMediaRecorder.setOnInfoListener(本);
//第5步:设置preVIEW输出
mMediaRecorder.set previewDisplay(M preview.getHolder()getSurface());
尝试{
mMediaRecorder prepare()。
}赶上(IllegalStateException异常五){
releaseMediaRecorder();
返回false;
}赶上(IOException异常五){
releaseMediaRecorder();
返回false;
}
返回true;
}
解决方案
您需要通过当前摄像机的ID,而使用的 凸轮coderProfile
。它将返回默认摄像头的轮廓,如果这种说法是不通过。
尝试,
<$p$p><$c$c>mMediaRecorder.setProfile(CamcorderProfile.get(camId,CamcorderProfile.QUALITY_TIME_LAPSE_480P));I have integrated camera which allows user to record time lapse videos.When the user is recording a video a cancel button is there which allows user to cancel the current recording.My issue is that if after cancelling i try to record again i get the following exception-
java.lang.NullPointerException: Attempt to read from field 'int android.media.CamcorderProfile.fileFormat' on a null object reference
at android.media.MediaRecorder.setProfile(MediaRecorder.java:423)
at xyz.fragments.CameraFragment.prepareFastVideoRecorder(CameraFragment.java:448)
at xyz.fragments.CameraFragment.access$2300(CameraFragment.java:64)
at xyz.fragments.CameraFragment$6.onClick(CameraFragment.java:329)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19761)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Method.java)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
on line
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_480P));
Below is my code-
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (prepareFastVideoRecorder()) {
mMediaRecorder.start();
} else {
releaseMediaRecorder();
}
}
}
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mMediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
mCamera.lock(); // take camera access back from
}
}):
private boolean prepareFastVideoRecorder() {
mMediaRecorder = new MediaRecorder();
try {
mCamera.setPreviewDisplay(null);
} catch (java.io.IOException ioe) {
Log.e("Bhuvnesh", "IOException nullifying preview display: " + ioe.getMessage());
}
mCamera.stopPreview();
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mMediaRecorder.setCamera(mCamera);
// Step 2: Set sources
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_480P));
//mMediaRecorder.setCaptureRate(0.1); // capture a frame every 10 seconds
mMediaRecorder.setCaptureRate(5); // capture a frame every 1 seconds
// Step 4: Set output file to the socket
mVideoFile = new File(getOutputMediaFile().toString());
mMediaRecorder.setOutputFile(mVideoFile.getAbsolutePath());
if (cameratype == 1)
mMediaRecorder.setOrientationHint(90);
else if (cameratype == 2)
mMediaRecorder.setOrientationHint(270);
mMediaRecorder.setMaxDuration(60000);
mMediaRecorder.setOnInfoListener(this);
// Step 5: Set the preview output
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
解决方案
You need to pass the current camera id while using get() in CamcoderProfile
. It will return the profile of default camera if this argument is not passed.
Try,
mMediaRecorder.setProfile(CamcorderProfile.get(camId,CamcorderProfile.QUALITY_TIME_LAPSE_480P));
这篇关于NullPointerException - 如果尝试取消后重新录制视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!