Android audio recording, part 1

Download source code of this article.

In Android we can record audio in two different ways; using MediaRecorder class and using AudioRecord class. Using MediaRecorder class is very easy but gives you less flexibility. AudioRecord class provides you more flexibility but is little bit complex. This article is about recording audio using MediaRecorder, I will explain the AudioRecord class in another article.

Using MediaRecorder class we can record the audio in two different formats, MediaRecorder.OutputFormat.THREE_GPP or MediaRecorder.OutputFormat.MPEG_4. In both cases the encoder should be MediaRecorder.AudioEncoder.AMR_NB. Till version 2.2, Android does not support other encoders. This link gives the details of media formats supported in Android.

To record an audio using MediaRecorder:

  1. Create an instance of MediaRecorder
  2. Set the audio source using setAudioSource() method
  3. Set output format using setOutputFormat() method
  4. Set Audio encoder using setAudioEncoder() method
  5. Set output file path using setOutputPath() method
  6. prepare using prepare() method
  7. start recording using start() method

The following code snippet starts audio recording:

recorder = new MediaRecorder();



try {
} catch (IllegalStateException e) {
} catch (IOException e) {

And following code snippet stops recording:


recorder = null;

Note that we have to reset and release the MediaRecorder object because it is a limited resource in Android, if it is not released other applications using MediaRecorder may not get this resource.

MediaRecorder class uses event listener interface to report any errors or warning occurred during recording session. There are two event listeners MediaRecorder.OnErrorListener and MediaRecorder.OnInfoListener. The MediaRecorder.OnErrorListener is used to report any errors during recording. It has the following method:

public void onError(MediaRecorder mr, int what, int extra)

This method is called when an error occurred, the first parameter is the MediaRecorder object, second one is the error code and third one is the extra information regarding the error occurred.

MediaRecorder.OnInfoListener is used to report any warning occurred. It has the following method:

public void onInfo(MediaRecorder mr, int what, int extra)

This method is called when any warning occurred during recording, the first parameter is the MediaRecorder object, second one is the warning code and third one is the extra information about the warning occurred.

The Audio Source can be one of the following values:

  1. MediaRecorder.AudioSource.DEFAULT – default source usually MIC
  2. MediaRecorder.AudioSource.MIC – Microphone
  3. MediaRecorder.AudioSource.VOICE_CALL – Voice call uplink and downlink source
  4. MediaRecorder.AudioSource.VOICE_DOWNLINK – Voice call downlink source
  5. MediaRecorder.AudioSource.VOICE_UPLINK – Voice call uplink source
  6. MediaRecorder.AudioSource.VOICE_RECOGNITION – Usually DEFAULT source

Even though we can set any one the above Audio Source, from my experience only MediaRecorder.AudioSource.MIC is working in my Nexus One with Android 2.2, all others are not working. If anyone can record using other sources please comment here.

Sample Application

The sample application is a simple one that records audio from MIC and stores the file in “/SDCard/AudioRecorder” folder with current milliseconds as the filename. A sample screen shot is displayed below:

Audio Recorder main screen Audio Recorder choose format screen

Hope this is piece of information is useful to you all.

40 Responses

  1. rojy george December 13, 2010 / 1:02 pm

    Hallo Sir,
    Downloaded the source code and created a sample app installed in device it records audio from MIC, but the recorded file is not playing shows “player does not support this type of audio file” .what should be done so that it will play in device please help

    • Krishnaraj Varma December 14, 2010 / 2:17 pm

      Hello Rojy, I am not sure why it is not playing in your device. Could you please check whether other 3GPP and MP4 files are playing, these formats are by the system.

    • Milind October 20, 2011 / 8:10 am

      Can we record Voice Call also with this method if we use : MediaRecorder.AudioSource.VOICE_CALL in place of MediaRecorder.AudioSource.MIC ?
      As per me when we select MediaRecorder.AudioSource.MIC then also it should work (only when input device is MIC) . In case if it is WIRED_HEADSET then MIC will not come into picture .
      Please let me know your inputs on “VOICE_CALL” point .

    • mukund November 24, 2011 / 9:57 am

      Hello sir, the file recorded are saved in amr format are not played, its give an error like “No suitable decoder module:
      Player does not support the audio or video format “samr”.” so where is the problem?
      Thanks in advance 🙂

  2. Poonam April 4, 2011 / 1:38 am

    I am having a small issue with the Media Record class. When I call the start() method, the recorder starts recording after some delay. I have seen this happening on Android 2.3 devices. This issue does not appear on Android 1.6 or any version of Android emulators. Could you please confirm if you are seeing this issue with your hardware and if you have any solution for the same?


    • Krishnaraj Varma April 10, 2011 / 7:16 am

      Poonam, I checked on my Nexus One with 2.3.3 and didn’t found any delay. Can you tell whether you are getting a long delay? also can you check in other devices with 2.3 also?

  3. hugh August 17, 2011 / 3:48 am

    I was really happy to read what you post on this website.
    I really wonder that I could make an app that records voice call
    with my voice and the others’.
    when I use those methods, I just can record my voice not
    the others’
    Could you help me with this code?

    public class RecordingAudioActivity extends Activity {
    private static final String AUDIO_RECORDER_FILE_EXT_3GP = “.3gp”;
    private static final String AUDIO_RECORDER_FILE_EXT_MP4 = “.mp4”;
    private static final String AUDIO_RECORDER_FILE_EXT_AMR = “.amr”;
    private static final String AUDIO_RECORDER_FOLDER = “AudioRecorder”;
    private Context context;
    private MediaRecorder recorder = null;
    private int currentFormat = 0;
    private int output_formats[] = { MediaRecorder.OutputFormat.MPEG_4, MediaRecorder.OutputFormat.THREE_GPP };
    AudioManager audiomanager;
    public void onCreate(Bundle savedInstanceState) {
    context = getApplicationContext();
    audiomanager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);


    private void setButtonHandlers() {

    private void enableButton(int id,boolean isEnable){

    private void enableButtons(boolean isRecording) {

    private void setFormatButtonCaption(){
    ((Button)findViewById( + ” (” + file_exts[currentFormat] + “)”);

    private String getFilename(){
    String filepath = Environment.getExternalStorageDirectory().getPath();
    File file = new File(filepath,AUDIO_RECORDER_FOLDER);


    return (file.getAbsolutePath() + “/” + System.currentTimeMillis() + file_exts[currentFormat]);

    private void startRecording(){
    recorder = new MediaRecorder();

    recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_DOWNLINK | MediaRecorder.AudioSource.VOICE_UPLINK);

    try {
    } catch (IllegalStateException e) {
    } catch (IOException e) {

    private void stopRecording(){
    if(null != recorder){

    recorder = null;

    private void displayFormatDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    String formats[] = {“MPEG 4”, “3GPP”, “AMR”};

    .setSingleChoiceItems(formats, currentFormat, new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {
    currentFormat = which;


    private MediaRecorder.OnErrorListener errorListener = new MediaRecorder.OnErrorListener() {
    public void onError(MediaRecorder mr, int what, int extra) {


    private MediaRecorder.OnInfoListener infoListener = new MediaRecorder.OnInfoListener() {
    public void onInfo(MediaRecorder mr, int what, int extra) {


    private View.OnClickListener btnClick = new View.OnClickListener() {
    public void onClick(View v) {






    • Meena July 25, 2012 / 10:13 am

      Can you tell what should i correct in your code ? Some errors are coming . I am facing some trouble could you help me ?

  4. sahap faras August 23, 2011 / 9:21 am

    it gives an error when i clicked to the “Record” button, i downloaded the source code. i dont know whats happening.

    • sahap faras August 23, 2011 / 9:33 am

      it’s about the permissions i’ve solved it.

      • Mathi June 16, 2012 / 1:21 pm

        Can u please tell me what permissions to be added….?

  5. Solanki Ranvir November 10, 2011 / 5:41 am

    Hello to all of you.
    I create my android application of streaming Radio.Now i am trying to record this streaming radio audio but i don’t know how it is done.which audio source i set instead of recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    So can any help me?

  6. Alan November 12, 2011 / 11:56 pm

    hello sahap, could you please tell me more about your solution? I am getting the same problem but I cannot find a way to fix it.

  7. Rashi November 29, 2011 / 12:37 pm

    Hello, can i start recording audio using Proximity Sensors? I mean when i bring my cell near mouth, it should start recording and after a certain time, it should stop? is it possible? I have found an option set max. duration for recording.. how can i do it? thanx in advance..

  8. rohit December 1, 2011 / 6:16 am

    Hi varma,
    I downloaded the source code and run that one start button is working but When click the stop button it showing the force close error.

  9. aru December 17, 2011 / 8:10 pm

    hii is dr a way fr me to stream this captured audio real time instead of recording

  10. krishna December 18, 2011 / 5:36 am

    hi sahap faras, can you let me know the permission that you have given and where? please share the piece of change tht you did….
    thanks in advance

    • Meera November 15, 2012 / 10:38 am

      These are the permissions required.

      Hope it helps

    • Meera November 15, 2012 / 10:40 am

      These are the permissions required
      hope it helps

  11. pramod December 22, 2011 / 6:47 am

    its really helpful for me

  12. john January 29, 2012 / 5:00 am

    hello, after I pasted your code to eclipse, i get this error

    The method onError(MediaRecorder, int, int) of type new MediaRecorder.OnErrorListener(){} must override a superclass method

    this error I ge on every line @Override
    public void onClick(DialogInterface dialog, int which) {
    currentFormat = which;

    Cpuld you help te by telling how to selve it?


  13. AuTi February 15, 2012 / 3:57 pm

    Hi, I have a little problem with the MediaRecorder.Start() statement.
    I’m develop a small application to record a call, but, when start to record the app stock in this state and I have to shut down the AVD, obviously, the file has no data.
    This is the code of my application.

    PS: Very great site…goes to favorite bar 🙂

  14. yogesh July 30, 2012 / 9:52 am

    Hello KRISHNARAJ, its really nice tutorial,& it help me a lot,
    i just want 2 ask when we will record a sound in mp4/3gp whatever, can we send dat file to any particular mail/number?

  15. babis p. January 2, 2013 / 12:55 am

    Hi all.

    Nice tutorial!
    Does anyone know if there is a way to automatically stop recording when the user stops speaking?

    Thanks in advance

  16. Ravi June 1, 2013 / 5:41 pm

    great tutorial.

  17. ANURAG June 19, 2013 / 3:49 pm

    the call recording code does not work for android 4 …it works out fine for android 2

  18. Hari Om July 1, 2013 / 9:07 am

    Can we store the recorded audio in raw wave format? I dont want to do any encoding over row audio, as I have to process it before to do encoding. What output format I should set for this application?

  19. Britto August 8, 2013 / 6:57 pm

    Hi sir, I am new to android… My problem is I need to record the voice and pause while recording after that resume recording also… Can we do in android.. I search lot but i can’t find a solution… Please help me ASAP..

  20. sara April 13, 2014 / 7:03 am

    i have an app that use Media recorder and i do like this sample!
    but it has a problem.on some device on start recording it crash with this error:
    start failed = -2147483648
    what is your suggestion.
    thanks in advanced

    • Britto April 21, 2014 / 8:27 am

      Check whether the memory card present r not. If present having enough space to start.. If having enough space, then check whether You Initialize the media recorder properlly.

  21. Britto April 21, 2014 / 8:29 am

    Check whether the memory card present r not. If present having enough space to start.. If having enough space, then check whether You Initialize the media recorder properly or not.

  22. Indra June 19, 2014 / 10:35 pm

    Hey, Thanks for such a nice post. Many people are looking for this kind of work. Highly appreciated. I have a query, how could we do it on a device without an SD card. I tried few options but nothing worked. Your comments are highly appreciated. Request you to share the code for it.

  23. Sagar September 3, 2014 / 11:59 am

    Nice tutorial.
    But was having issue. If I put FM radio ON and then try to record voice the app gets crashed saying runtimeexception : start failed i.e at start() it fails.
    Pls help me.

  24. Parth October 27, 2014 / 3:15 pm


    have you managed to record voice call?

  25. alex February 8, 2016 / 3:25 pm

    When I try to use VOICE_DOWNLINK

    I always get exception in stop method.

    02-08 10:53:25.390 18200-18200/com.terrificmobile.callconnectionalert E/MediaRecorder: stop failed: -1007
    02-08 10:53:25.390 18200-18200/com.terrificmobile.callconnectionalert D/AndroidRuntime: Shutting down VM
    02-08 10:53:25.390 18200-18200/com.terrificmobile.callconnectionalert E/AndroidRuntime: FATAL EXCEPTION: main
    02-08 10:53:25.390 18200-18200/com.terrificmobile.callconnectionalert E/AndroidRuntime: Process: com.terrificmobile.callconnectionalert, PID: 18200
    02-08 10:53:25.390 18200-18200/com.terrificmobile.callconnectionalert E/AndroidRuntime: java.lang.RuntimeException: stop failed.
    02-08 10:53:25.390 18200-18200/com.terrificmobile.callconnectionalert E/AndroidRuntime: at Method)

    but everything works well with VOICE_CALL

    This is how I start recording
    mr = new MediaRecorder();


    Does anyone happen to know why?


  26. Nitish Kumar February 12, 2016 / 4:09 pm

    Really this is useful code for all of us. I have one query, I am making app for noise suppression.. I have to record only audio/voice not a noise. So how can we do so. If you have any idea then please help me.

  27. Dilip September 28, 2016 / 4:45 pm

    I am creating a audio recorder using MediaRecorder.
    I am using AMR_NB as AudioEncoder and it gives good sound quality audio but does not support on IOS device.what should I do?
    If I am using AAC as AudioEncoder it supports IOS device but sound quality is poor.
    what should I do to get good sound quality audio and support both for Android and IOS,

Leave a Reply

Your email address will not be published. Required fields are marked *