Audio Capture

August 5,2016

Android has a built in microphone through which you can capture audio and store it , or play it in your phone. There are many ways to do that but the most common way is through MediaRecorder class.

Android provides MediaRecorder class to record audio or video. In order to use MediaRecorder class ,you will first create an instance of MediaRecorder class. Its syntax is given below.

MediaRecorder myAudioRecorder = new MediaRecorder();

Now you will set the source , output and encoding format and output file. Their syntax is given below.

myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);

After specifying the audio source and format and its output file, we can then call the two basic methods prepare and start to start recording the audio.

myAudioRecorder.prepare();
myAudioRecorder.start();

Apart from these methods , there are other methods listed in the MediaRecorder class that allows you more control over audio and video recording.

Sr.No Method & description
1 setAudioSource()

This method specifies the source of audio to be recorded

2 setVideoSource()

This method specifies the source of video to be recorded

3 setOutputFormat()

This method specifies the audio format in which audio to be stored

4 setAudioEncoder()

This method specifies the audio encoder to be used

5 setOutputFile()

This method configures the path to the file into which the recorded audio is to be stored

6 stop()

This method stops the recording process.

7 release()

This method should be called when the recorder instance is needed.

Leave a comment