Nullpointerexception如果尝试在取消后再次录制视频

时间:2015-10-18 15:38:24

标签: android nullpointerexception android-camera android-mediarecorder timelapse

我集成了相机,允许用户录制定时视频。当用户录制视频时,会有取消按钮,允许用户取消当前录制。我的问题是,如果取消后我尝试再次录制得到以下异常 -

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)

在线

mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_480P));

以下是我的代码 -

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;
}

1 个答案:

答案 0 :(得分:2)

您需要在CamcoderProfile中使用get()时传递当前的相机ID。如果未传递此参数,它将返回默认摄像机的配置文件。

尝试,

mMediaRecorder.setProfile(CamcorderProfile.get(camId,CamcorderProfile.QUALITY_TIME_LAPSE_480P));