Exemplo n.º 1
0
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private boolean prepareVideoRecorder() {

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

    Camera.Parameters parameters = mCamera.getParameters();
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
    mCamera.setDisplayOrientation(90);
    mCamera.setParameters(parameters);

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

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

    // Camera.Parameters parameters = mCamera.getParameters();

    mMediaRecorder.setCamera(mCamera);
    CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_QVGA);

    // Log.d(TAG, profile.videoBitRate+"<- videoBitRate r");
    profile.videoBitRate = 1024000;

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

    // Step 3: Set all values contained in profile except audio settings
    mMediaRecorder.setOutputFormat(profile.fileFormat);
    mMediaRecorder.setVideoEncoder(profile.videoCodec);
    mMediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
    mMediaRecorder.setVideoFrameRate(profile.videoFrameRate);
    mMediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
    mMediaRecorder.setOrientationHint(90);
    mMediaRecorder.setVideoFrameRate(24);

    // Step 4: Set output file
    save_to = CameraHelper.getOutputMediaFile(CameraHelper.MEDIA_TYPE_VIDEO).toString();
    mMediaRecorder.setOutputFile(save_to);

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

    // 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;
  }
  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;
    }
  }