示例#1
0
  public void startRecording(View view) {
    try {
      if (!Utils.isIntentAvailable(this, MediaStore.ACTION_VIDEO_CAPTURE)) {
        Toast.makeText(this, "No camera available", Toast.LENGTH_SHORT).show();
        return;
      }

      camera.unlock();
      recorder = new MediaRecorder();
      recorder.setCamera(camera);

      recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

      recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_480P));
      // no need to set setOutputFormat, setAudioEncoder, setVideoEncoder
      // when I set the profile instead?

      recorder.setOutputFile(Utils.getOutputMediaFilePath(Utils.MEDIA_TYPE_VIDEO));

      recorder.setPreviewDisplay(cameraPreview.getHolder().getSurface());

      recorder.prepare();
      recorder.start();

      Log.i(TAG, "Recording started");

    } catch (Exception e) {
      releaseMediaRecorder();
      Log.e(TAG, e.getMessage());
      e.printStackTrace();
    }
  }
 private boolean prepareVideoRecorder() {
   if (mCamera == null) {
     mCamera = getCameraInstance();
   }
   mMediaRecorder = new MediaRecorder();
   // Step 1: Unlock and set camera to MediaRecorder
   mCamera.unlock();
   mMediaRecorder.setCamera(mCamera);
   // Step 2: Set sources
   mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
   mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
   // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
   mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
   // Step 4: Set output file
   mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
   // Step 5: Set the preview output
   mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
   // Step 6: 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;
    }
  }