5

我正在尝试开发带有前后摄像头的录像机。该应用程序可以在后置(默认)和前置摄像头之间切换。当应用程序激活前置摄像头时,我按下录制按钮,应用程序进入错误方法,我释放并初始化相机和录制,但错误再次出现。任何想法?更新:我用下面的代码更新了 onError 方法,我认为我没有很好地发布和初始化相机,因为当应用程序执行此方法时,表面支架是黑色的并且没有给我什么相机看到

在这里,我的代码:

onError 方法:

 public void onError(MediaRecorder mr, int what, int extra) {
    stopRecording();

    //if Error 100, app must release the Camera object and instantiate a new one.
    if (what == 100) {
        if (error_100  == 2) { //Error 100 persists, change camera
            if (Camera.getNumberOfCameras() <= 1) {
                //No mas camaras para app
            } else {
                if (selected_camera == FRONT_CAMERA) {
                    selected_camera=BACK_CAMERA;
                } else {
                    selected_camera=FRONT_CAMERA;
                }
                selected_camera_button.setEnabled(false);
                Toast.makeText(this, "Initializing other camera", Toast.LENGTH_SHORT).show();
            }

        } else { //No camera init finish activity
            if (error_100 == 3) {
                Toast.makeText(this, "Initialize cameras failed", Toast.LENGTH_SHORT).show();
                finish();
            } else {
                Toast.makeText(this, "Recording error has occurred. Stopping the recording", Toast.LENGTH_SHORT).show();
            }
        }
        error_100++;

        initCamera();
        initCameraRecorder();
    }
}

还有另一种方法:

/** Initializes the back/front camera */
private boolean initCamera() {
    try {
        camera  = getCameraInstance(selected_camera);

        Camera.Parameters camParams = camera.getParameters();
        camParams.set( "cam_mode", 1 );
        camParams.set("orientation", "portrait");

        checkCameraFlash(camParams);

        //Establish the rotation angle camera
        setAngleCameraRotation();
        camera.setDisplayOrientation(angle_rotation_camera);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        Establish 4:3 ratio and 480x640 if is posible
        setAspectResolutionCamera(camParams);

        camera.setParameters(camParams);

        //Camera eye
        video_view.getHolder().setFixedSize(height_video, width_video);


        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(height_video, width_video);
        video_view.setLayoutParams(lp);

        camera.lock();

        surface_holder = video_view.getHolder();
        surface_holder.addCallback(this);
        surface_holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        setPreviewCamera();

    } catch(Exception e) {
        Log.v("RecordVideo", "Could not initialize the Camera");
        return false;
    }
    return true;
}

/** Initializes the camera recorder before starts the recording */
private void initCameraRecorder() {

    if(media_recorder != null) return;

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    output_file_name = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + timeStamp + ".mp4";

    File outFile = new File(output_file_name);
    if(outFile.exists()) {
        outFile.delete();
    }

    try {
        camera.stopPreview();
        camera.unlock();
        media_recorder = new MediaRecorder();
        media_recorder.setCamera(camera);

        media_recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        media_recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
        profile.videoBitRate = 885000;              //Medium quality
        profile.videoFrameHeight = height_video;
        profile.videoFrameWidth = width_video;
        media_recorder.setProfile(profile);

        media_recorder.setMaxDuration(21000);       // limit to 21 seconds
        media_recorder.setOrientationHint(setOrientationCameraRecorder());
        media_recorder.setPreviewDisplay(surface_holder.getSurface());
        media_recorder.setOutputFile(output_file_name);

        media_recorder.prepare();
        Log.v("RecordVideo", "MediaRecorder initialized");

    } catch(Exception e) {
        Log.v("RecordVideo", "MediaRecorder failed to initialize");
        e.printStackTrace();
    }
}

/** Starts the recording video */
private void beginRecording() {
    media_recorder.setOnInfoListener(this);
    media_recorder.setOnErrorListener(this);
    media_recorder.start();
    record_button.setTextColor(getResources().getColor(android.R.color.holo_red_dark));
}

/** Stops the recording video */
private void stopRecording() {
    if (media_recorder != null) {
        media_recorder.setOnErrorListener(null);
        media_recorder.setOnInfoListener(null);
        try {
            media_recorder.stop();
        } catch(IllegalStateException e) {
            // This can happen if the recorder has already stopped.
            Log.e("INFO:", "Got IllegalStateException in stopRecording");
        }
        releaseRecorder();
        record_button.setTextColor(getResources().getColor(android.R.color.black));
        releaseCamera();
    }

    video_task.cancel(true);

    output_file_name.isEmpty();

}
4

0 回答 0