0

我是一个新的安卓开发者。我已经使用照相机完成了一个 Activity 以在表面视图中拍照,但现在我正在考虑在 Activity 中添加一个按钮,以便在同一表面视图中将相机切换到录像机。这个有可能?

提前致谢。

4

1 回答 1

0

我有自己问题的解决方案。在我认为放置一个按钮以在具有相同表面视图的同一个活动中录制视频之后,我为照片相机做了一个表面视图和一个活动,但我不知道这是否可能。嗯,我在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();

    }

}

如果你需要,我希望能帮助你。

于 2013-04-09T19:01:19.680 回答