@Override /** M: the error callback of MediaRecorder */ public void onError(MediaRecorder recorder, int errorType, int extraCode) { LogUtils.i(TAG, "<onError> errorType = " + errorType + "; extraCode = " + extraCode); stopRecording(); mListener.onError(this, ErrorHandle.ERROR_RECORDING_FAILED); }
public boolean pauseRecording() { if ((SoundRecorderService.STATE_RECORDING != mCurrentState) || (null == mRecorder)) { mListener.onError(this, SoundRecorderService.STATE_ERROR_CODE); return false; } try { MediaRecorderEx.pause(mRecorder); } catch (IllegalArgumentException e) { LogUtils.e(TAG, "<pauseRecording> IllegalArgumentException"); handleException(false, e); mListener.onError(this, ErrorHandle.ERROR_RECORDING_FAILED); return false; } mPreviousTime += SystemClock.elapsedRealtime() - mSampleStart; setState(SoundRecorderService.STATE_PAUSE_RECORDING); return true; }
private boolean createRecordingFile(String extension) { LogUtils.i(TAG, "<createRecordingFile> begin"); String myExtension = extension + SAMPLE_SUFFIX; File sampleDir = null; if (null == mStorageManager) { return false; } sampleDir = new File(StorageManagerEx.getDefaultPath()); LogUtils.i(TAG, "<createRecordingFile> sd card directory is:" + sampleDir); String sampleDirPath = sampleDir.getAbsolutePath() + File.separator + RECORD_FOLDER; sampleDir = new File(sampleDirPath); // find a available name of recording folder, // Recording/Recording(1)/Recording(2) int dirID = 1; while ((null != sampleDir) && sampleDir.exists() && !sampleDir.isDirectory()) { sampleDir = new File(sampleDirPath + '(' + dirID + ')'); dirID++; } if ((null != sampleDir) && !sampleDir.exists() && !sampleDir.mkdirs()) { LogUtils.i( TAG, "<createRecordingFile> make directory [" + sampleDir.getAbsolutePath() + "] fail"); } boolean isCreateSuccess = true; try { if (null != sampleDir) { LogUtils.i(TAG, "<createRecordingFile> sample directory is:" + sampleDir.toString()); } SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String time = simpleDateFormat.format(new Date(System.currentTimeMillis())); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(SAMPLE_PREFIX).append(time).append(myExtension); String name = stringBuilder.toString(); mSampleFile = new File(sampleDir, name); isCreateSuccess = mSampleFile.createNewFile(); LogUtils.i(TAG, "<createRecordingFile> creat file success is " + isCreateSuccess); LogUtils.i( TAG, "<createRecordingFile> mSampleFile.getAbsolutePath() is: " + mSampleFile.getAbsolutePath()); } catch (IOException e) { mListener.onError(this, ErrorHandle.ERROR_CREATE_FILE_FAILED); LogUtils.e(TAG, "<createRecordingFile> io exception happens"); e.printStackTrace(); isCreateSuccess = false; } finally { LogUtils.i(TAG, "<createRecordingFile> end"); return isCreateSuccess; } }
public boolean stopRecording() { LogUtils.i(TAG, "<stopRecording> start"); if (((SoundRecorderService.STATE_PAUSE_RECORDING != mCurrentState) && (SoundRecorderService.STATE_RECORDING != mCurrentState)) || (null == mRecorder)) { LogUtils.i(TAG, "<stopRecording> end 1"); mListener.onError(this, SoundRecorderService.STATE_ERROR_CODE); return false; } boolean isAdd = (SoundRecorderService.STATE_RECORDING == mCurrentState) ? true : false; synchronized (this) { try { if (mCurrentState != SoundRecorderService.STATE_IDLE) { mRecorder.stop(); } } catch (RuntimeException exception) { /** M:modified for stop recording failed. @{ */ handleException(false, exception); mListener.onError(this, ErrorHandle.ERROR_RECORDING_FAILED); LogUtils.e(TAG, "<stopRecording> recorder illegalstate exception in recorder.stop()"); } finally { if (null != mRecorder) { mRecorder.reset(); mRecorder.release(); mRecorder = null; } if (isAdd) { mPreviousTime += SystemClock.elapsedRealtime() - mSampleStart; } mSampleLength = mPreviousTime; LogUtils.i(TAG, "<stopRecording> mSampleLength in ms is " + mPreviousTime); LogUtils.i(TAG, "<stopRecording> mSampleLength in s is = " + mSampleLength); setState(SoundRecorderService.STATE_IDLE); } /** @} */ } LogUtils.i(TAG, "<stopRecording> end 2"); return true; }
public boolean goonRecording() { if ((SoundRecorderService.STATE_PAUSE_RECORDING != mCurrentState) || (null == mRecorder)) { return false; } try { mRecorder.start(); } catch (IllegalArgumentException exception) { LogUtils.e(TAG, "<goOnRecording> IllegalArgumentException"); exception.printStackTrace(); mRecorder.reset(); mRecorder.release(); mRecorder = null; mListener.onError(this, ErrorHandle.ERROR_RECORDING_FAILED); return false; } mSampleStart = SystemClock.elapsedRealtime(); setState(SoundRecorderService.STATE_RECORDING); return true; }
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 void setState(int state) { mCurrentState = state; mListener.onStateChanged(this, state); }