Android:将照相机切换到同一表面上的摄像机

时间:2013-04-09 10:59:12

标签: android-camera

我是一名新的Android开发人员。 我用照相机做了一个活动,在表面视图中拍照,但现在我想在活动中添加一个按钮,在相同的表面视图中将相机切换到录像机。这可能吗?

先谢谢。

1 个答案:

答案 0 :(得分:0)

我有自己的问题的解决方案。在我想用相同的表面视图在同一个Activity中录制视频的按钮后,我做了一个表面视图和一个照相机的活动,但我不知道是否可能。好吧,我在Activity中编写了这个方法来准备de MediaRecorder并采用表面视图。

    public Boolean prepararCamaraVideo(){

    mMediaRecorder = new MediaRecorder();

    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

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

    state = MediaRecorderState.INITIALIZED;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO)
        mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
    else { 
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    }

    state = MediaRecorderState.DATA_SOURCE_CONFIGURED;

    mOutputFile = Files.getExternalMediaFile(Files.MEDIA_TYPE_VIDEO).toString();
    mMediaRecorder.setOutputFile(mOutputFile);

    mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder().getSurface());

    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        Log.d("Video", "IllegalStateException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;

    } catch (IOException e) {
        Log.d("Video", "IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    }
    return true;

}

此命令mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder()。getSurface());获得摄像机的表面。

最后记录视频的方法。

    public void grabaVideo(View v) {
    if (state!=MediaRecorderState.RECORDING){
        if (prepararCamaraVideo()) {
            mMediaRecorder.start();
            state = MediaRecorderState.RECORDING;
            Toast.makeText(getApplicationContext(), getString(R.string.capturing_video), Toast.LENGTH_SHORT).show();                
        } else {
            // prepare didn't work, release the camera
            releaseMediaRecorder();
            // inform user
        }
    }
    else{
        mMediaRecorder.stop();  // stop the recording
        releaseMediaRecorder(); // release the MediaRecorder object
        mCamera.lock();         // take camera access back from MediaRecorder

        state = MediaRecorderState.INITIAL;
        Toast.makeText(getApplicationContext(), getString(R.string.video_stored_in) + " " + mOutputFile, Toast.LENGTH_SHORT).show();

    }

}

如果您需要,我希望能帮到您。