0

我集成了摄像头,允许用户录制延时视频。当用户录制视频时,有一个取消按钮,允许用户取消当前录制。我的问题是,如果取消后我尝试再次录制,我会得到以下信息例外-

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

1 回答 1

2

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

尝试,

mMediaRecorder.setProfile(CamcorderProfile.get(camId,CamcorderProfile.QUALITY_TIME_LAPSE_480P));
于 2015-10-19T10:51:04.307 回答