Example #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() {
    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;
  }
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
      if (!_isRecording) {
        _recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        _recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        _recorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

        _recorder.setVideoEncodingBitRate(10000000);

        _recorder.setVideoFrameRate(5);

        _recorder.setVideoSize(100, 100);

        _recorder.setOutputFile("/sdcard/sample.3gp");
        _recorder.setPreviewDisplay(_holder.getSurface());

        try {
          _recorder.prepare();
        } catch (Exception e) {
          Log.e("test", "recorder error");
        }
        _recorder.start();
        _isRecording = true;

      } else {
        _recorder.stop();
        _recorder.reset();
        _isRecording = false;
      }
    }
    return true;
  }
 private void startRecording() {
   if (cameraBusy) return;
   System.out.println("start - lock enabled");
   cameraBusy = true;
   mCamera.unlock();
   recorder = new MediaRecorder();
   recorder.setCamera(mCamera);
   recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
   CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
   recorder.setOutputFormat(profile.fileFormat);
   recorder.setVideoFrameRate(profile.videoFrameRate);
   recorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
   recorder.setVideoEncodingBitRate(profile.videoBitRate);
   recorder.setVideoEncoder(profile.videoCodec);
   recorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
   recorder.setPreviewDisplay(mHolder.getSurface());
   try {
     recorder.prepare();
     recorder.start();
     recording = true;
     System.out.println("start - lock disabled");
   } catch (IllegalStateException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
   cameraBusy = false;
 }
  @SuppressLint("NewApi")
  private boolean initRecorder() {
    if (!EaseCommonUtils.isExitsSdcard()) {
      showNoSDCardDialog();
      return false;
    }

    if (mCamera == null) {
      if (!initCamera()) {
        showFailDialog();
        return false;
      }
    }
    mVideoView.setVisibility(View.VISIBLE);
    // TODO init button
    mCamera.stopPreview();
    mediaRecorder = new MediaRecorder();
    mCamera.unlock();
    mediaRecorder.setCamera(mCamera);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    // 设置录制视频源为Camera(相机)
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    if (frontCamera == 1) {
      mediaRecorder.setOrientationHint(270);
    } else {
      mediaRecorder.setOrientationHint(90);
    }
    // 设置录制完成后视频的封装格式THREE_GPP为3gp.MPEG_4为mp4
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    // 设置录制的视频编码h263 h264
    mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    // 设置视频录制的分辨率。必须放在设置编码和格式的后面,否则报错
    mediaRecorder.setVideoSize(previewWidth, previewHeight);
    // 设置视频的比特率
    mediaRecorder.setVideoEncodingBitRate(384 * 1024);
    // // 设置录制的视频帧率。必须放在设置编码和格式的后面,否则报错
    if (defaultVideoFrameRate != -1) {
      mediaRecorder.setVideoFrameRate(defaultVideoFrameRate);
    }
    // 设置视频文件输出的路径
    localPath = PathUtil.getInstance().getVideoPath() + "/" + System.currentTimeMillis() + ".mp4";
    mediaRecorder.setOutputFile(localPath);
    mediaRecorder.setMaxDuration(30000);
    mediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
    try {
      mediaRecorder.prepare();
    } catch (IllegalStateException e) {
      e.printStackTrace();
      return false;
    } catch (IOException e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }
Example #6
0
  private void configureMediaRecorder() {
    mediaRecorder.setCamera(camera);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

    mediaRecorder.setVideoSize(1280, 720);
    mediaRecorder.setVideoEncodingBitRate(17000000);
    mediaRecorder.setVideoFrameRate(30);

    mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
  }
 /**
  * Uses the settings from a CamcorderProfile object for recording. This method should be called
  * after the video AND audio sources are set, and before setOutputFile(). If a time lapse
  * CamcorderProfile is used, audio related source or recording parameters are ignored.
  *
  * @param profile the CamcorderProfile to use
  * @see android.media.CamcorderProfile
  */
 public void setProfile(CamcorderProfile profile) {
   setOutputFormat(profile.fileFormat);
   setVideoFrameRate(profile.videoFrameRate);
   setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
   setVideoEncodingBitRate(profile.videoBitRate);
   setVideoEncoder(profile.videoCodec);
   if (profile.quality >= CamcorderProfile.QUALITY_TIME_LAPSE_LOW
       && profile.quality <= CamcorderProfile.QUALITY_TIME_LAPSE_QVGA) {
     // Nothing needs to be done. Call to setCaptureRate() enables
     // time lapse video recording.
   } else {
     setAudioEncodingBitRate(profile.audioBitRate);
     setAudioChannels(profile.audioChannels);
     setAudioSamplingRate(profile.audioSampleRate);
     setAudioEncoder(profile.audioCodec);
   }
 }
Example #8
0
  private boolean startRecording() {
    prCamera.stopPreview();
    try {
      prCamera.unlock();
      prMediaRecorder.setCamera(prCamera);
      // set audio source as Microphone, video source as camera
      // state: Initial=>Initialized
      // prMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      prMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
      // set the file output format: 3gp or mp4
      // state: Initialized=>DataSourceConfigured

      String lDisplayMsg = "Current container format: ";

      if (Utils.puContainerFormat == SettingsDialog.cpu3GP) {
        lDisplayMsg += "3GP\n";
        videoFormat = ".3gp";
        prMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      } else if (Utils.puContainerFormat == SettingsDialog.cpuMP4) {
        lDisplayMsg += "MP4\n";
        videoFormat = ".mp4";
        prMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
      } else {
        lDisplayMsg += "3GP\n";
        videoFormat = ".3gp";
        prMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      }

      lDisplayMsg += "Current encoding format: ";
      if (Utils.puEncodingFormat == SettingsDialog.cpuH263) {
        lDisplayMsg += "H263\n";
        prMediaRecorder.setVideoEncoder(VideoEncoder.H263);
      } else if (Utils.puEncodingFormat == SettingsDialog.cpuMP4_SP) {
        lDisplayMsg += "MPEG4-SP\n";
        prMediaRecorder.setVideoEncoder(VideoEncoder.MPEG_4_SP);
      } else if (Utils.puEncodingFormat == SettingsDialog.cpuH264) {
        lDisplayMsg += "H264\n";
        prMediaRecorder.setVideoEncoder(VideoEncoder.H264);
      } else {
        lDisplayMsg += "H263\n";
        prMediaRecorder.setVideoEncoder(VideoEncoder.H263);
      }
      videoFileFullPath =
          myVideoFilePath + String.valueOf(System.currentTimeMillis()) + videoFormat;
      prRecordedFile = new File(videoFileFullPath);
      prMediaRecorder.setOutputFile(prRecordedFile.getPath());
      if (Utils.puResolutionChoice == SettingsDialog.cpuRes176) {
        prMediaRecorder.setVideoSize(176, 144);
      } else if (Utils.puResolutionChoice == SettingsDialog.cpuRes320) {
        prMediaRecorder.setVideoSize(320, 240);
      } else if (Utils.puResolutionChoice == SettingsDialog.cpuRes720) {
        prMediaRecorder.setVideoSize(720, 480);
      }
      Toast.makeText(prContext, lDisplayMsg, Toast.LENGTH_LONG).show();
      prMediaRecorder.setVideoFrameRate(cFrameRate);
      prMediaRecorder.setPreviewDisplay(prSurfaceHolder.getSurface());
      prMediaRecorder.setMaxFileSize(cMaxFileSizeInBytes);
      // prepare for capturing
      // state: DataSourceConfigured => prepared
      prMediaRecorder.prepare();
      // start recording
      // state: prepared => recording
      prMediaRecorder.start();
      btnStartRecorder.setText("Stop");
      prRecordInProcess = true;
      return true;
    } catch (IOException _le) {
      _le.printStackTrace();
      return false;
    }
  }
 private boolean startRecording() {
   prCamera.stopPreview();
   try {
     prCamera.unlock();
     prMediaRecorder.setCamera(prCamera);
     // set audio source as Microphone, video source as camera
     // state: Initial=>Initialized
     prMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
     prMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
     // set the file output format: 3gp or mp4
     // state: Initialized=>DataSourceConfigured
     String lDisplayMsg = "Current container format: ";
     int lContainerFormat = SettingsStatic.getContainerFormat(this.getApplicationContext());
     if (lContainerFormat == SettingsDialog.cpu3GP) {
       lDisplayMsg += "3GP\n";
       mVideoFileFullPath = ".3gp";
       prMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
     } else if (lContainerFormat == SettingsDialog.cpuMP4) {
       lDisplayMsg += "MP4\n";
       mVideoFileFullPath = ".mp4";
       prMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
     } else {
       lDisplayMsg += "3GP\n";
       mVideoFileFullPath = ".3gp";
       prMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
     }
     // the encoders:
     // audio: AMR-NB
     // prMediaRecorder.setAudioEncoder(AudioEncoder.AMR_NB);
     prMediaRecorder.setAudioEncoder(AudioEncoder.AAC);
     // video: H.263, MP4-SP, or H.264
     // prMediaRecorder.setVideoEncoder(VideoEncoder.H263);
     // prMediaRecorder.setVideoEncoder(VideoEncoder.MPEG_4_SP);
     lDisplayMsg += "Current encoding format: ";
     int lEncodingFormat = SettingsStatic.getEncodingFormat(getApplicationContext());
     if (lEncodingFormat == SettingsDialog.cpuH263) {
       lDisplayMsg += "H263\n";
       prMediaRecorder.setVideoEncoder(VideoEncoder.H263);
     } else if (lEncodingFormat == SettingsDialog.cpuMP4_SP) {
       lDisplayMsg += "MPEG4-SP\n";
       prMediaRecorder.setVideoEncoder(VideoEncoder.MPEG_4_SP);
     } else if (lEncodingFormat == SettingsDialog.cpuH264) {
       lDisplayMsg += "H264\n";
       prMediaRecorder.setVideoEncoder(VideoEncoder.H264);
     } else {
       lDisplayMsg += "H263\n";
       prMediaRecorder.setVideoEncoder(VideoEncoder.H263);
     }
     mVideoFileFullPath =
         FileUtilsStatic.DEFAULT_DIR
             + DateTimeUtilsStatic.get_current_date_time_format1_str()
             + mVideoFileFullPath;
     prRecordedFile = new File(mVideoFileFullPath);
     prMediaRecorder.setOutputFile(prRecordedFile.getPath());
     int lRes = SettingsStatic.getResolutionChoice(this.getApplicationContext());
     if (lRes == SettingsDialog.cpuRes176) {
       prMediaRecorder.setVideoSize(176, 144);
     } else if (lRes == SettingsDialog.cpuRes320) {
       prMediaRecorder.setVideoSize(320, 240);
     } else if (lRes == SettingsDialog.cpuRes720) {
       prMediaRecorder.setVideoSize(720, 480);
     }
     lDisplayMsg += "Video recording to file: " + mVideoFileFullPath;
     Toast.makeText(prContext, lDisplayMsg, Toast.LENGTH_LONG).show();
     prMediaRecorder.setVideoFrameRate(cFrameRate);
     prMediaRecorder.setPreviewDisplay(prSurfaceHolder.getSurface());
     prMediaRecorder.setMaxDuration(cMaxRecordDurationInMs);
     prMediaRecorder.setMaxFileSize(cMaxFileSizeInBytes);
     // prepare for capturing
     // state: DataSourceConfigured => prepared
     prMediaRecorder.prepare();
     // start recording
     // state: prepared => recording
     prMediaRecorder.start();
     prStartBtn.setText("Stop");
     prRecordInProcess = true;
     return true;
   } catch (IOException _le) {
     _le.printStackTrace();
     return false;
   }
 }