Android audio recording, part 1

Standard

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();

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/sdcard/sample.3gp");

recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);

try {
	recorder.prepare();
	recorder.start();
} catch (IllegalStateException e) {
	e.printStackTrace();
} catch (IOException e) {
	e.printStackTrace();
}

And following code snippet stops recording:

recorder.stop();
recorder.reset();
recorder.release();

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.

35 thoughts on “Android audio recording, part 1

  1. rojy george

    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

    • Milind

      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

      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

    Hello,
    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?

    Thanks

  3. hugh

    HI!
    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 };
    private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP, AUDIO_RECORDER_FILE_EXT_AMR};
    AudioManager audiomanager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    context = getApplicationContext();
    audiomanager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

    setButtonHandlers();
    enableButtons(false);
    setFormatButtonCaption();
    }

    private void setButtonHandlers() {
    ((Button)findViewById(R.id.btnStart)).setOnClickListener(btnClick);
    ((Button)findViewById(R.id.btnStop)).setOnClickListener(btnClick);
    ((Button)findViewById(R.id.btnFormat)).setOnClickListener(btnClick);
    }

    private void enableButton(int id,boolean isEnable){
    ((Button)findViewById(id)).setEnabled(isEnable);
    }

    private void enableButtons(boolean isRecording) {
    enableButton(R.id.btnStart,!isRecording);
    enableButton(R.id.btnFormat,!isRecording);
    enableButton(R.id.btnStop,isRecording);
    }

    private void setFormatButtonCaption(){
    ((Button)findViewById(R.id.btnFormat)).setText(getString(R.string.audio_format) + ” (” + file_exts[currentFormat] + “)”);
    }

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

    if(!file.exists()){
    file.mkdirs();
    }

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

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

    recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_DOWNLINK | MediaRecorder.AudioSource.VOICE_UPLINK);
    //recorder.setOutputFormat(output_formats[currentFormat]);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(getFilename());
    //recorder.setAudioChannels(1);
    //recorder.setAudioSamplingRate(44100);
    //audiomanager.setSpeakerphoneOn(true);
    recorder.setOnErrorListener(errorListener);
    recorder.setOnInfoListener(infoListener);

    try {
    recorder.prepare();
    recorder.start();
    } catch (IllegalStateException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    private void stopRecording(){
    if(null != recorder){
    recorder.stop();
    recorder.reset();
    recorder.release();

    recorder = null;
    }
    }

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

    builder.setTitle(getString(R.string.choose_format_title))
    .setSingleChoiceItems(formats, currentFormat, new DialogInterface.OnClickListener() {

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

    dialog.dismiss();
    }
    })
    .show();
    }

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

    }
    };

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

    }
    };

    private View.OnClickListener btnClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    switch(v.getId()){
    case R.id.btnStart:{

    enableButtons(true);
    startRecording();

    break;
    }
    case R.id.btnStop:{

    enableButtons(false);
    stopRecording();

    break;
    }
    case R.id.btnFormat:{
    displayFormatDialog();

    break;
    }
    }
    }
    };
    }

  4. sahap faras

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

  5. Solanki Ranvir

    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. Rashi

    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..

  7. rohit

    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.

  8. krishna

    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

  9. john

    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;
    setFormatButtonCaption();

    Cpuld you help te by telling how to selve it?

    thanks!

  10. 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.

    https://gist.github.com/1829663

    PS: Very great site…goes to favorite bar :)

  11. yogesh

    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?

  12. babis p.

    Hi all.

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

    Thanks in advance

  13. Hari Om

    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?

  14. Britto

    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..

  15. Hi,
    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

      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.

  16. Britto

    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.

  17. Indra

    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.

  18. Sagar

    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.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>