Skip to content

Instantly share code, notes, and snippets.

@yccheok
Created February 21, 2019 11:52
Show Gist options
  • Select an option

  • Save yccheok/84a1ea5809c7d7a4fa4be8f9bb4a3863 to your computer and use it in GitHub Desktop.

Select an option

Save yccheok/84a1ea5809c7d7a4fa4be8f9bb4a3863 to your computer and use it in GitHub Desktop.
private void startMediaRecorder() {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioChannels(1);
mediaRecorder.setAudioSamplingRate(8000);
mediaRecorder.setAudioEncodingBitRate(32000);
/*
mediaRecorder.setAudioChannels(1);
mediaRecorder.setAudioSamplingRate(44100);
mediaRecorder.setAudioEncodingBitRate(96000);
*/
mediaRecorder.setMaxDuration(RECORDING_MEDIA_RECORDER_MAX_DURATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
} else {
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
}
mediaRecorder.setOutputFile(getMicFilepath());
mediaRecorder.setOnInfoListener((mediaRecorder, what, i1) -> {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
dismiss();
}
});
try {
mediaRecorder.prepare();
} catch (IOException e) {
Log.e(TAG, "", e);
}
mediaRecorder.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment