Пример #1
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
  }
Пример #2
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;
  }
  @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;
      }
    }
  }
Пример #4
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;
    }
  }
Пример #5
0
 private boolean initAndStartMediaRecorder(RecordParams recordParams, int fileSizeLimit) {
   LogUtils.i(TAG, "<initAndStartMediaRecorder> start");
   try {
     /**
      * M:Changed to catch the IllegalStateException and NullPointerException. And the
      * IllegalStateException will be caught and handled in RuntimeException .@{
      */
     mSelectEffect = recordParams.mAudioEffect;
     mRecorder = new MediaRecorder();
     mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
     mRecorder.setOutputFormat(recordParams.mOutputFormat);
     mRecorder.setOutputFile(mSampleFile.getAbsolutePath());
     if (RecordParamsSetting.canSelectMode()) {
       MediaRecorderEx.setHDRecordMode(mRecorder, recordParams.mHDRecordMode, false);
     }
     /** M:Add for create/activate/delete AudioEffect at native layer. @{ */
     if (RecordParamsSetting.canSelectEffect()) {
       int iSelEffects = 0;
       if (mSelectEffect[RecordParamsSetting.EFFECT_AEC]) {
         iSelEffects |= (1 << RecordParamsSetting.EFFECT_AEC);
       }
       if (mSelectEffect[RecordParamsSetting.EFFECT_NS]) {
         iSelEffects |= (1 << RecordParamsSetting.EFFECT_NS);
       }
       if (mSelectEffect[RecordParamsSetting.EFFECT_AGC]) {
         iSelEffects |= (1 << RecordParamsSetting.EFFECT_AGC);
       }
       MediaRecorderEx.setPreprocessEffect(mRecorder, iSelEffects);
     }
     /** @} */
     mRecorder.setAudioEncoder(recordParams.mAudioEncoder);
     mRecorder.setAudioChannels(recordParams.mAudioChannels);
     mRecorder.setAudioEncodingBitRate(recordParams.mAudioEncodingBitRate);
     mRecorder.setAudioSamplingRate(recordParams.mAudioSamplingRate);
     if (fileSizeLimit > 0) {
       mRecorder.setMaxFileSize(fileSizeLimit);
     }
     mRecorder.setOnErrorListener(this);
     /** @}* */
     mRecorder.prepare();
     mRecorder.start();
   } catch (IOException exception) {
     LogUtils.e(TAG, "<initAndStartMediaRecorder> IO exception");
     // M:Add for when error ,the tmp file should been delete.
     handleException(true, exception);
     mListener.onError(this, ErrorHandle.ERROR_RECORDING_FAILED);
     return false;
   }
   /**
    * M: used to catch the null pointer exception in ALPS01226113, and never show any toast or
    * dialog to end user. Because this error just happened when fast tapping the file list button
    * after tapping record button(which triggered by tapping the recording button in audio play
    * back view).@{
    */
   catch (NullPointerException exception) {
     handleException(true, exception);
     return false;
   }
   /** @} */
   catch (RuntimeException exception) {
     LogUtils.e(TAG, "<initAndStartMediaRecorder> RuntimeException");
     // M:Add for when error ,the tmp file should been delete.
     handleException(true, exception);
     mListener.onError(this, ErrorHandle.ERROR_RECORDER_OCCUPIED);
     return false;
   }
   LogUtils.i(TAG, "<initAndStartMediaRecorder> end");
   return true;
 }
 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;
   }
 }