相机未正确释放

时间:2014-04-30 21:20:46

标签: java android android-camera android-mediarecorder

在某种程度上,我在此活动中使用的Camera似乎未正确发布。我为什么这么说?因为如果我退出应用程序并打开设备的原生相机应用程序,它会崩溃。然后,如果我卸载我的应用程序,它可以正常工作。

活动使用相机录制视频,使用以下课程:CameraMediaRecorderTextureView

以下是活动的相关代码:

onCreate()中初始化TextureView:

TextureView textureView = (TextureView) findViewById(R.id.textureview);
textureView.setSurfaceTextureListener(this);


OnSurfaceTextureAvailable 方法:

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    mCamera = Camera.open();
    mCamera.setPreviewTexture(surface);
    mCamera.startPreview();
    isPreviewRunning = true;
}


OnSurfaceTextureUpdated 方法:

@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    if (!isRecording) {
        if (isPreviewRunning){
            mCamera.stopPreview();
            mCamera.lock();
        }

        Camera.Parameters parameters = mCamera.getParameters();

        Size highest_size = parameters.getSupportedPreviewSizes().get(0);
        int[] fps_range = parameters.getSupportedPreviewFpsRange().get(0);

        parameters.setPreviewSize(highest_size.width, highest_size.height);
        parameters.setPreviewFpsRange(fps_range[0], fps_range[1]);

        mCamera.setParameters(parameters);

        mCamera.setPreviewTexture(surface);
        mCamera.startPreview();
        isPreviewRunning = true;    

        prepareRecorder(surface);   
    }
}


PrepareRecorder 方法:

private void prepareRecorder(SurfaceTexture surface) {
    mRecorder = new MediaRecorder();

    mCamera.unlock();
    mRecorder.setCamera(mCamera);
    mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    // Other settings, using CamcorderProfile.QUALITY_HIGH

    mRecorder.prepare();
}


OnSurfaceTextureDestroyed 方法:

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    if (isPreviewRunning) {
        mCamera.stopPreview();
        isPreviewRunning = false;
    }

    if (isRecording) {
        mRecorder.stop();
        isRecording = false;
    }

    mRecorder.release();
    mCamera.release();

    mRecorder = null;
    mCamera = null;

    return true;
}

0 个答案:

没有答案