Nullpointerexception if try to record video again after cancelling

。_饼干妹妹 提交于 2019-12-11 16:32:28

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/33199834/nullpointerexception-if-try-to-record-video-again-after-cancelling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!