Reputation: 14370
I need to record sound by using mobile's own microphone... How to do it?
Upvotes: 4
Views: 9305
Reputation: 4408
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();
Upvotes: 4
Reputation: 3855
It's explained here
Audio capture from the device is a bit more complicated than audio/video playback, but still fairly simple:
- Create a new instance of android.media.MediaRecorder using new
- Set the audio source using MediaRecorder.setAudioSource(). You will probably want to use MediaRecorder.AudioSource.MIC
- Set output file format using MediaRecorder.setOutputFormat()
- Set output file name using MediaRecorder.setOutputFile()
- Set the audio encoder using MediaRecorder.setAudioEncoder()
- Call MediaRecorder.prepare() on the MediaRecorder instance.
- To start audio capture, call MediaRecorder.start().
- To stop audio capture, call MediaRecorder.stop().
- When you are done with the MediaRecorder instance, call MediaRecorder.release() on it. Calling MediaRecorder.release() is always recommended to free the resource immediately.
Upvotes: 10