private static Camera.Size chooseVideoSize(BaseCaptureInterface ci, List<Camera.Size> choices) {
   Camera.Size backupSize = null;
   for (Camera.Size size : choices) {
     if (size.height <= ci.videoPreferredHeight()) {
       if (size.width == size.height * ci.videoPreferredAspect()) return size;
       if (ci.videoPreferredHeight() >= size.height) backupSize = size;
     }
   }
   if (backupSize != null) return backupSize;
   LOG(CameraFragment.class, "Couldn't find any suitable video size");
   return choices.get(choices.size() - 1);
 }
  private boolean prepareMediaRecorder(int forceQuality) {
    try {
      final Activity activity = getActivity();
      if (null == activity) return false;
      final BaseCaptureInterface captureInterface = (BaseCaptureInterface) activity;

      setCameraDisplayOrientation(mCamera.getParameters());
      mMediaRecorder = new MediaRecorder();
      mCamera.stopPreview();
      mCamera.unlock();
      mMediaRecorder.setCamera(mCamera);

      Log.d(
          "Camera1Fragment",
          String.format(
              "Bit rate: %d, Frame rate: %d, Resolution: %s",
              captureInterface.videoBitRate(),
              captureInterface.videoFrameRate(),
              String.format(Locale.getDefault(), "%dx%d", mVideoSize.width, mVideoSize.height)));

      mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
      mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
      CamcorderProfile camcorderProfile = CamcorderProfile.get(getCurrentCameraId(), forceQuality);
      camcorderProfile.videoBitRate = 1024000;
      mMediaRecorder.setProfile(camcorderProfile);
      mMediaRecorder.setVideoSize(mVideoSize.width, mVideoSize.height);
      if (captureInterface.videoFrameRate() > 0)
        mMediaRecorder.setVideoFrameRate(captureInterface.videoFrameRate());
      if (captureInterface.videoBitRate() > 0)
        mMediaRecorder.setVideoEncodingBitRate(captureInterface.videoBitRate());
      Uri uri = Uri.fromFile(getOutputMediaFile());
      mOutputUri = uri.toString();
      mMediaRecorder.setOutputFile(uri.getPath());

      mMediaRecorder.setOrientationHint(mDisplayOrientation);
      mMediaRecorder.setPreviewDisplay(mPreviewView.getHolder().getSurface());

      try {
        mMediaRecorder.prepare();
        return true;
      } catch (Throwable e) {
        throwError(new Exception("Failed to prepare the media recorder: " + e.getMessage(), e));
        return false;
      }
    } catch (Throwable t) {
      try {
        mCamera.lock();
      } catch (IllegalStateException e) {
        throwError(new Exception("Failed to re-lock camera: " + e.getMessage(), e));
        return false;
      }
      if (forceQuality == CamcorderProfile.QUALITY_480P)
        return prepareMediaRecorder(CamcorderProfile.QUALITY_720P);
      else if (forceQuality == CamcorderProfile.QUALITY_720P)
        return prepareMediaRecorder(CamcorderProfile.QUALITY_LOW);
      else if (forceQuality == CamcorderProfile.QUALITY_LOW) {
        return prepareMediaRecorder(CamcorderProfile.QUALITY_1080P);
      }
      throwError(new Exception("Failed to begin recording: " + t.getMessage(), t));
      return false;
    }
  }