1

我有一个使用本机摄像头的应用程序,我需要制作一个视频,然后切换摄像头,然后再制作一个来附加它们。问题是,如果我随后释放相机,当我录制第二个视频时,它会说:在 release() 之后无法调用方法。如果我不释放相机,那么在更换第二台相机时会显示:无法连接到相机服务。

这是切换相机的功能:

 cameraFlipButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (recording) {
                    wasrecording = true;
                }
                LogService.log(TAG, "--------------SWITCH CAMERA PRESSED");
                LogService.log(TAG, "Camera: " + mCameraDevice.getCamera().getParameters() + "camera id: " + mCameraId);
                if (mCameraId == 0) {
                    mCameraId = 1;
                    mCurrentModule.mPendingSwitchCameraId = 1;
                } else if (mCameraId == 1) {
                    mCameraId = 0;
                    mCurrentModule.mPendingSwitchCameraId = 0;
                }
                if (recording) {
                    recording = false;
                    recordButton.setImageResource(R.drawable.shutter);
                    if (timer != null) {
                        LogService.log(TAG, "---------------Timer2: " + timer);
                        timer.cancel();
                        timer.purge();
                    }
                    timer = null;
                    Handler han = new Handler();
                    han.postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            mCurrentModule.switchCamera();

                        }
                    }, 200);

                } else {
                    mCurrentModule.switchCamera();
                }
            }
        });

这是我的相机管理器切换相机的功能(mCurrentModule.switchCamera()):

public void switchCamera() {
    if (mPaused)
        return;

    Log.i(TAG, "MAMAMAMAMAMAM Start to switch camera. VIDEO MODULE (switchCamera)");
    mCameraId = mPendingSwitchCameraId;
    mPendingSwitchCameraId = -1;
    setCameraId(mCameraId);
    Log.i(TAG, "MAMAMAMAMAMAM after settting camera id VIDEO MODULE (switchCamera)");

    CameraHolder.instance().release();
    Log.i(TAG, "MAMAMAMAMAMAM after release VIDEO MODULE (switchCamera)");
    videoFragment.mCameraDevice = null;
    mPreviewing = false;
    Log.i(TAG, "MAMAMAMAMAMAM before open camera VIDEO MODULE (switchCamera)");
    openCamera();
    Log.i(TAG, "MAMAMAMAMAMAM after open camera VIDEO MODULE (switchCamera)");
    getDesiredPreviewSize();
    Log.i(TAG, "MAMAMAMAMAMAM after get desired size VIDEO MODULE (switchCamera)");
    startPreview();
    Log.i(TAG, "MAMAMAMAMAMAM after start preview VIDEO MODULE (switchCamera)");
    resizeForPreviewAspectRatio();
    Log.i(TAG, "MAMAMAMAMAMAM before handler VIDEO MODULE (switchCamera)");
    if (ApiHelper.HAS_SURFACE_TEXTURE) {
        // Start switch camera animation. Post a message because
        // onFrameAvailable from the old camera may already exist.
        mHandler.sendEmptyMessage(SWITCH_CAMERA_START_ANIMATION);
    }
}

CameraHolder.instance().release();从此函数调用处理程序以释放执行此操作的相机:

public synchronized void release() {

    if (mCameraDevice == null)
        return;

    mCameraOpened = false;
    mCameraDevice.release();
    mCameraDevice = null;
    // We must set this to null because it has a reference to Camera.
    // Camera has references to the listeners.
    mParameters = null;
    mCameraId = -1;
}

释放后我再次打开相机:

public synchronized CameraProxy open(int cameraId) throws CameraHardwareException {
    // Log.i(TAG,
    // "MAMAMAMAMAMAM before release from open CAMERA HOLDER open() CameraProxy");
    if (mCameraOpened) {
        Log.i(TAG, "MAMAMAMAMAMAM before release CAMERA OPENED CAMERA HOLDER open() CameraProxy ");
        mCameraDevice.release();
    }
    if (mCameraDevice != null) {
        Log.i(TAG, "MAMAMAMAMAMAM before release from open mCameraId != cameraId CAMERA HOLDER open() CameraProxy");
        mCameraDevice.release();
        mCameraDevice = null;
        mCameraId = -1;
    }
    if (mCameraDevice == null) {
        try {
            Log.i(TAG, "MAMAMAMAMAMAM open camera " + cameraId);
            if (mMockCameraInfo == null) {
                mCameraDevice = CameraManager.instance().cameraOpen(cameraId);
            } else {
                if (mMockCamera == null)
                    throw new RuntimeException();
                mCameraDevice = mMockCamera[cameraId];
            }
            mCameraId = cameraId;
        } catch (RuntimeException e) {
            Log.e(TAG, "fail to connect Camera", e);
            throw new CameraHardwareException(e);
        }
        mParameters = mCameraDevice.getParameters();
    } else {
        try {
            mCameraDevice.reconnect();
        } catch (IOException e) {
            Log.e(TAG, "reconnect failed.");
            throw new CameraHardwareException(e);
        }
        mCameraDevice.setParameters(mParameters);
    }
    mCameraOpened = true;
    mHandler.removeMessages(RELEASE_CAMERA);
    mKeepBeforeTime = 0;
    return mCameraDevice;
}

在此之后,我获得了屏幕的大小,设置预览等。之后,如果我再次按下录制按钮,它将因“无法在 release() 后调用方法”而崩溃。现在如果我不释放,它会在切换相机时崩溃,因为它无法连接到相机服务。有没有解决的办法?我怎样才能让它释放相机并仍然设法记录?

4

1 回答 1

0

在切换摄像头功能中,openCamera();调用后我再次初始化了记录器,如下所示:

recorderHelper.init(videoFragment.mCameraDevice.getCamera(), mPreviewSurfaceView.getHolder());

现在它可以工作了,我设法更换相机和记录。

于 2013-09-03T11:37:52.460 回答