예제 #1
0
  private boolean prepareMediaRecorder() {
    myCamera = getCameraInstance(switchCam);
    Camera.Parameters parameters = myCamera.getParameters();
    parameters.setFlashMode(getFlashModeSetting());
    if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
      myCamera.setDisplayOrientation(90);
    } else {
      myCamera.setDisplayOrientation(0);
    }

    myCamera.setParameters(parameters);
    myCamera.unlock();

    mediaRecorder = new MediaRecorder();
    mediaRecorder.setCamera(myCamera);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    if (switchCam == 1) {
      mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
      mediaRecorder.setVideoFrameRate(15);
    } else {
      mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
      mediaRecorder.setVideoFrameRate(30);
    }

    if (switchCam == 0) {
      if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
        mediaRecorder.setOrientationHint(90);
      } else {
        mediaRecorder.setOrientationHint(0);
      }
    } else {
      mediaRecorder.setOrientationHint(270);
    }

    mediaRecorder.setOutputFile("/sdcard/" + customVideoPath + ".mp4");
    mediaRecorder.setMaxDuration(240000); // Set max duration 4 mins.
    mediaRecorder.setMaxFileSize(8000000); // Set max file size 8M
    mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());

    try {
      mediaRecorder.prepare();
    } catch (IllegalStateException e) {
      releaseMediaRecorder();
      return false;
    } catch (IOException e) {
      releaseMediaRecorder();
      return false;
    }
    return true;
  }
예제 #2
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;
 }
예제 #4
0
  private void initRecorder() {
    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);

    CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    recorder.setProfile(cpHigh);
    recorder.setOutputFile("/sdcard/videocapture_example.mp4");
    recorder.setMaxDuration(50000); // 50 seconds
    recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
  }
  @Override
  public void recordVideo(CameraSession session, VideoTransaction xact) throws Exception {
    Descriptor descriptor = (Descriptor) session.getDescriptor();
    Camera camera = descriptor.getCamera();

    if (camera != null) {
      camera.stopPreview();
      camera.unlock();

      try {
        recorder = new MediaRecorder();
        recorder.setCamera(camera);
        recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        int cameraId = descriptor.getCameraId();
        boolean canGoHigh = CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_HIGH);
        boolean canGoLow = CamcorderProfile.hasProfile(cameraId, CamcorderProfile.QUALITY_LOW);

        if (canGoHigh && (xact.getQuality() == 1 || !canGoLow)) {
          recorder.setProfile(CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_HIGH));
        } else if (canGoLow) {
          recorder.setProfile(CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_LOW));
        } else {
          throw new IllegalStateException("cannot find valid CamcorderProfile");
        }

        recorder.setOutputFile(xact.getOutputPath().getAbsolutePath());
        recorder.setMaxFileSize(xact.getSizeLimit());
        recorder.setMaxDuration(xact.getDurationLimit());
        recorder.setOnInfoListener(this);
        // recorder.setOrientationHint(...);
        recorder.prepare();
        recorder.start();
        this.xact = xact;
      } catch (IOException e) {
        recorder.release();
        recorder = null;
        throw e;
      }
    }
  }
예제 #6
0
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  private boolean prepareVideoRecorder() {
    mCamera = tbs.beamr.android.sid.CamHelper.getDefaultCameraInstance();
    final Camera.Parameters parameters = mCamera.getParameters();
    List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
    if (sizes == null || sizes.size() < 1) {
      sizes = parameters.getSupportedVideoSizes();
    }

    Log.e("supported sizes > ", getSizeString(sizes));
    final Camera.Size size = getCameraSize(sizes);

    switch (hasAutoFocus(parameters)) {
      case 0:
        break;
      case 1:
        parameters.set("cam_mode", 1);
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        break;
      case 2:
        parameters.set("cam_mode", 1);
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
        break;
    }

    mCamera.setParameters(parameters);
    try {
      mCamera.setPreviewDisplay(mPreview.getHolder());
    } catch (IOException e) {
      Log.e(TAG, "Surface View is unavailable or unsuitable" + e.getMessage());
      return false;
    }
    mMediaRecorder = new MediaRecorder();

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

    // Step 2: Set sources
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    mMediaRecorder.setOrientationHint(90);

    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    final CamcorderProfile profile =
        CamcorderProfile.get(
            CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_480P)
                ? CamcorderProfile.QUALITY_480P
                : CamcorderProfile.QUALITY_HIGH);
    //        profile.videoFrameRate = 25;
    //            profile.videoBitRate = 7200000;
    mMediaRecorder.setProfile(profile);
    //        if (size == null) {
    //            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    //            final CamcorderProfile profile =
    // CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
    ////            profile.videoFrameRate = 30;
    ////            profile.videoBitRate = 7200000;
    //            mMediaRecorder.setProfile(profile);
    //        } else {
    //            // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    //            if (CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_720P)) {
    //                final CamcorderProfile profile =
    // CamcorderProfile.get(CamcorderProfile.QUALITY_720P);
    //                Log.e("recordingSize > ", String.valueOf(profile.videoFrameWidth) + "x" +
    // String.valueOf(profile.videoFrameHeight));
    //                mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    //                mMediaRecorder.setProfile(profile);
    //            } else {
    //                mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    //                mMediaRecorder.setVideoEncodingBitRate(8200000);
    //                mMediaRecorder.setVideoSize(size.width, size.height);
    ////            mMediaRecorder.setVideoFrameRate(20);
    //                mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
    //            }
    //        }

    // Step 4: Set output file
    // Todo
    //        final String name = Environment.getExternalStorageDirectory().getAbsolutePath() +
    // "/tbs_beamR/received_file_" + (new Date(System.currentTimeMillis())).toString() + ".mp4";
    final String name =
        Environment.getExternalStorageDirectory().getAbsolutePath() + "/test/test.mp4";
    final File file = new File(name);
    if (!file.exists()) {
      try {
        file.getParentFile().mkdirs();
        //                file.createNewFile();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    mMediaRecorder.setOutputFile(name);

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