How to record audio file in Android
It is easy to record audio in Android. What you need to do is:
1) Create the object for media record class : MediaRecorder recorder = new MediaRecorder();
2) In the emulator, you're unable to store the recorded data in memory, so you have to store it on the SD Card. So, first check for the SD Card availability: then start recording with the following code.
String status = Environment.getExternalStorageState();
if(status.equals("mounted")){
String path = your path;
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
3) To stop, override stop method of activity
recorder.stop();
recorder.release();
Here is a good tutorial with sample code.
Audio Capture at Android Developer
it includes these steps:
- Create a new instance of android.media.MediaRecorder.
- 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.