/**
  * 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);
   }
 }
  /** Starts a new recording. */
  public boolean startRecording(int seconds) {
    isRecordingStart = true;
    // String state = android.os.Environment.getExternalStorageState();
    // if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
    // throw new IOException("SD Card is not mounted.  It is " + state +
    // ".");
    // }
    try {
      // make sure the directory we plan to store the recording in exists
      File directory = new File(path).getParentFile();
      if (!directory.exists() && !directory.mkdirs()) {
        isRecordingStart = false;
        throw new IOException("Path to file could not be created.");
      }

      recorder = new MediaRecorder();
      recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

      if (MEDIA_TYPE.equals(".amr")) {
        recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
      } else {
        // recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioChannels(1);
        // recorder.setAudioSamplingRate(SAMPLE_RATE);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        // recorder.setAudioEncodingBitRate(AUDIO_BITRATE);

      }

      recorder.setOutputFile(path);

      // recorder.setMaxDuration(seconds * 1000);
      recorder.prepare();
      recorder.start();
      recorder.setOnErrorListener(this);
      recorder.setOnInfoListener(this);
    } catch (Exception ex) {
      ex.printStackTrace();
      isRecordingStart = false;
      return false;
    }
    return true;
  }
  /**
   * Default constructor
   *
   * <p>Instantiates a new recorder, in case of compressed recording the parameters can be left as
   * 0. In case of errors, no exception is thrown, but the state is set to ERROR
   */
  public MediaRecorderWrapper() {
    try {
      mRecorder = new MediaRecorder();
      mRecorder.setAudioEncodingBitRate(24000);
      mRecorder.setAudioChannels(1);
      mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
      mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

      fPath = null;
      state = State.INITIALIZING;
    } catch (Exception e) {
      if (e.getMessage() != null) {
        SurespotLog.e(MediaRecorderWrapper.class.getName(), e, e.getMessage());
      } else {
        SurespotLog.e(
            MediaRecorderWrapper.class.getName(),
            e,
            "Unknown error occured while initializing recording");
      }
      state = State.ERROR;
    }
  }
Ejemplo n.º 4
0
 /**
  * 开始录音
  *
  * @version 1.0
  * @author zyh
  * @param chatId 会话Id
  */
 public void startRecording(String chatId) {
   if (recoder == null) {
     recoder = new MediaRecorder();
     recoder.setAudioSource(1);
     recoder.setOutputFormat(3);
     recoder.setAudioEncoder(1);
     recoder.setAudioChannels(1);
     recoder.setAudioEncodingBitRate(12200);
     recoder.setOnErrorListener(new RecorderErrorListener());
   } else {
     recoder.stop();
     recoder.reset();
   }
   recordFileName = System.currentTimeMillis() + ".amr";
   recordFilePath = getRecordFilePath(chatId);
   file = new File(recordFilePath);
   recoder.setOutputFile(file.getAbsolutePath());
   try {
     recoder.prepare();
     recoder.start();
     atomicBoolean.set(true);
     startTime = new Date().getTime();
     executor.execute(new CalculateRunnable(this));
     return;
   } catch (IllegalStateException localIllegalStateException) {
     Log.i("voice", "IllegalStateException thrown while trying to record a greeting");
     atomicBoolean.set(false);
     recoder.release();
     recoder = null;
     return;
   } catch (IOException localIOException) {
     Log.i("voice", "IOException thrown while trying to record a greeting");
     atomicBoolean.set(false);
     recoder.release();
     recoder = null;
   }
 }
Ejemplo n.º 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;
 }