How to record sound using the built-in microphone in Android

I need to record sound using a mobile microphone ... How to do this?

+4
source share
2 answers

He explained here

Recording audio from a device is a bit more complicated than audio / video playback, but still quite simple:

  • Create a new instance of android.media.MediaRecorder using the new
  • Set the sound source using MediaRecorder.setAudioSource (). You probably want to use MediaRecorder.AudioSource.MIC
  • Set the output file format using MediaRecorder.setOutputFormat ()
  • Set output file name using MediaRecorder.setOutputFile ()
  • Install an audio encoder using MediaRecorder.setAudioEncoder ()
  • Call MediaRecorder.prepare () on the MediaRecorder instance.
  • To start recording audio, call MediaRecorder.start ().
  • To stop capturing audio, call MediaRecorder.stop ().
  • When you are done with the MediaRecorder instance, call MediaRecorder.release () on it. calling MediaRecorder.release () is always recommended to release the resource immediately.
+10
source

Example:

To start recording:

MediaRecorder audioRecorder = new MediaRecorder(); audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); audioRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); audioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); audioRecorder.setOutputFile(AUDIO_FILE_PATH); try { audioRecorder.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } audioRecorder.start(); 

To stop recording:

  audioRecorder.stop(); audioRecorder.release(); 
+4
source

All Articles