Skip to content

Instantly share code, notes, and snippets.

@syalam
Created July 21, 2015 22:05
Show Gist options
  • Save syalam/6912a98424e78ff43674 to your computer and use it in GitHub Desktop.
Save syalam/6912a98424e78ff43674 to your computer and use it in GitHub Desktop.

Revisions

  1. syalam created this gist Jul 21, 2015.
    72 changes: 72 additions & 0 deletions snippet.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private boolean prepareVideoRecorder(){

    // BEGIN_INCLUDE (configure_preview)
    mCamera = CameraHelper.getDefaultCameraInstance();

    // We need to make sure that our preview and recording video size are supported by the
    // camera. Query camera to find all the sizes and choose the optimal size given the
    // dimensions of our preview surface.
    Camera.Parameters parameters = mCamera.getParameters();
    List<Camera.Size> mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();
    Camera.Size optimalSize = CameraHelper.getOptimalPreviewSize(mSupportedPreviewSizes,
    mPreview.getWidth(), mPreview.getHeight());

    // likewise for the camera object itself.
    parameters.setPreviewSize(optimalSize.width, optimalSize.height);

    parameters.set("fast-fps-mode", 2);

    mCamera.setParameters(parameters);
    try {
    // Requires API level 11+, For backward compatibility use {@link setPreviewDisplay}
    // with {@link SurfaceView}
    mCamera.setPreviewTexture(mPreview.getSurfaceTexture());
    } catch (IOException e) {
    Log.e(TAG, "Surface texture is unavailable or unsuitable" + e.getMessage());
    return false;
    }
    // END_INCLUDE (configure_preview)


    // BEGIN_INCLUDE (configure_media_recorder)
    mMediaRecorder = new MediaRecorder();

    // Use the same size for recording profile.
    // CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_1080P);
    // profile.videoFrameWidth = optimalSize.width;
    // profile.videoFrameHeight = optimalSize.height;

    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    // Step 2: Set sources
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    mMediaRecorder.setVideoSize(optimalSize.width, optimalSize.height);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    mMediaRecorder.setVideoFrameRate(120);

    // Step 4: Set output file
    mMediaRecorder.setOutputFile(mFile.toString());

    // END_INCLUDE (configure_media_recorder)

    // Step 5: Prepare configured MediaRecorder
    try {
    mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
    Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
    releaseMediaRecorder();
    return false;
    } catch (IOException e) {
    Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
    releaseMediaRecorder();
    return false;
    }
    return true;
    }