Android Toggle camera illegalstateexception

时间:2014-06-11 13:09:20

标签: android android-camera

我在相机工作。我用切换相机。当我在后置摄像头录制视频时,每个想法都没问题,但是当我使用前置摄像头时,我有illegalstateexception。我尝试更改我的stopRecording()方法,但没有任何改变。 这是我的来源

prSurfaceHolder = prSurfaceView.getHolder();
    prSurfaceHolder.addCallback(this);
    prSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);




toogleCamera.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (choose == false) {
                try {
                    toggleCamera();

                } catch (IOException e) {
                    e.printStackTrace();
                }
                choose = true;
            } else {
                try {

                    BackCamera();

                } catch (IOException e) {

                    e.printStackTrace();
                }
                choose = false;

            }
        }
    });




public void toggleCamera() throws IOException {
    prCamera.stopPreview();
    int frontCameraId = 0;
    for (int camNo = 0; camNo < Camera.getNumberOfCameras(); camNo++) {
        CameraInfo camInfo = new CameraInfo();
        Camera.getCameraInfo(camNo, camInfo);
        if (camInfo.facing == (Camera.CameraInfo.CAMERA_FACING_FRONT)) {
            frontCameraId = camNo;
        }
    }
    prCamera.release();
    // Log.d(TAG + "Restarting camera", ""+frontCameraId);
    prCamera = Camera.open(frontCameraId);
    // added code

    prCamera.setPreviewDisplay(prSurfaceHolder);
    // prCamera.setPreviewCallback([reference to your Camera.PreviewCallback
    // object]);
    // end added code
    prCamera.startPreview();
    // Log.d(TAG + "Restarting camera", ""+frontCameraId);

}

public void BackCamera() throws IOException {
    prCamera.stopPreview();
    int frontCameraId = 0;
    for (int camNo = 0; camNo < Camera.getNumberOfCameras(); camNo++) {
        CameraInfo camInfo = new CameraInfo();
        Camera.getCameraInfo(camNo, camInfo);
        if (camInfo.facing == (Camera.CameraInfo.CAMERA_FACING_BACK)) {
            frontCameraId = camNo;
        }
    }
    prCamera.release();
    // Log.d(TAG + "Restarting camera", ""+frontCameraId);
    prCamera = Camera.open(frontCameraId);
    // added code

    prCamera.setPreviewDisplay(prSurfaceHolder);
    // prCamera.setPreviewCallback([reference to your Camera.PreviewCallback
    // object]);
    // end added code
    prCamera.startPreview();
    // Log.d(TAG + "Restarting camera", ""+frontCameraId);

}




@Override
public void surfaceChanged(SurfaceHolder _holder, int _format, int _width,
        int _height) {
    try {
        if (prCamera == null)
            return;

        prCamera.setPreviewDisplay(_holder);
        prCamera.startPreview();
    } catch (IOException e) {

        e.printStackTrace();
    }
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    CameraInfo cameraInfo = new CameraInfo();
    int cameraCount = Camera.getNumberOfCameras();
    if (cameraCount > 1)
        for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); camIdx++) {
            Camera.getCameraInfo(camIdx, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                try {
                    prCamera = Camera.open(camIdx);
                } catch (RuntimeException e) {
                    Log.i("Camera failed to open: ",
                            e.getLocalizedMessage());
                }
            }
        }
    else
        prCamera = Camera.open();

    if (prCamera == null) {
        Toast.makeText(this, "Camera is not available!", Toast.LENGTH_SHORT)
                .show();
        finish();
    }

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

    if (prCamera != null) {
        prCamera.stopPreview();

        prCamera.stopPreview();
        prCamera.release();
        prCamera = null;
    }

}

protected void startRecording() throws IOException {
    prMediaRecorder = new MediaRecorder();
    prCamera.unlock();

    prMediaRecorder.setCamera(prCamera);

    prMediaRecorder.setPreviewDisplay(prSurfaceHolder.getSurface());
    prMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    prMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);

    prMediaRecorder.setProfile(CamcorderProfile
            .get(CamcorderProfile.QUALITY_HIGH));
    prMediaRecorder.setPreviewDisplay(prSurfaceHolder.getSurface());
    prMediaRecorder.setOutputFile(mVideoFilename);

    prMediaRecorder.prepare();
    prMediaRecorder.start();

}

private void stopRecording() {
    if (prMediaRecorder != null) {

        prMediaRecorder.stop();
        timer.cancel();

        prMediaRecorder.reset();
        prMediaRecorder.release();
        video_start_timer.setText("00:15");

    }

}

我不知道我做错了什么。 如果有人知道解决方案,请帮助我。

0 个答案:

没有答案