Android Audio Recorder

The Android multimedia framework includes support for recording and encoding a variety of common audio formats, so that you can easily integrate audio into your applications.

MediaRecorder is used to record the audio
MediaPlayer is used to play the recorded audio

The Android Emulator does not have the ability to record audio, but actual devices are likely to provide these capabilities.


  • 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 through MediaRecorder.setOutputFormat().
  • Set output file name through MediaRecorder.setOutputFile().
  • Set the audio encoder through MediaRecorder.setAudioEncoder().
  • Call MediaRecorder.prepare() on the MediaRecorder instance.
  • To start audio record, call MediaRecorder.start().
  • To stop audio record, 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.

    You can download source code here.

    This is code for AudioRecorderActivity.It is used to record and play the audio.

    package com.etr.audiorecorderdemo;

    import java.io.IOException;

    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.media.MediaRecorder;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class AudioRecorderActivity extends Activity {
    Button record, play;
    private static final String LOG_TAG = "AudioRecorderActivity";
    private static String mFileName = null;

    private MediaRecorder mRecorder = null;
    private MediaPlayer mPlayer = null;

    boolean mStartRecording = true;
    boolean mStartPlaying = true;

    public AudioRecorderActivity() {
    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
    mFileName += "/audiorecorddemo.3gp";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    try {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_audio_recorder);

    record = (Button) findViewById(R.id.button1);
    play = (Button) findViewById(R.id.button3);

    record.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    onRecord(mStartRecording);
    if (mStartRecording) {
    record.setText("Stop recording");
    } else {
    record.setText("Start recording");
    }
    mStartRecording = !mStartRecording;
    }
    });

    play.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    onPlay(mStartPlaying);
    if (mStartPlaying) {
    play.setText("Stop playing");
    } else {
    play.setText("Start playing");
    }
    mStartPlaying = !mStartPlaying;
    }
    });

    } catch (Exception e) {
    Log.v("Exception", Log.getStackTraceString(e));
    }

    }

    private void onRecord(boolean start) {
    if (start) {
    startRecording();
    } else {
    stopRecording();
    }
    }

    private void onPlay(boolean start) {
    if (start) {
    startPlaying();
    } else {
    stopPlaying();
    }
    }

    private void startPlaying() {
    mPlayer = new MediaPlayer();
    try {
    mPlayer.setDataSource(mFileName);
    mPlayer.prepare();
    mPlayer.start();
    } catch (IOException e) {
    Log.e(LOG_TAG, "prepare() failed");
    }
    }

    private void stopPlaying() {
    mPlayer.release();
    mPlayer = null;
    }

    private void startRecording() {
    mRecorder = new MediaRecorder();
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mRecorder.setOutputFile(mFileName);
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    try {
    mRecorder.prepare();
    } catch (IOException e) {
    Log.e(LOG_TAG, "prepare() failed");
    }

    mRecorder.start();
    }

    private void stopRecording() {
    mRecorder.stop();
    mRecorder.release();
    mRecorder = null;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.audio_recorder, menu);
    return true;
    }

    @Override
    public void onPause() {
    super.onPause();
    if (mRecorder != null) {
    mRecorder.release();
    mRecorder = null;
    }

    if (mPlayer != null) {
    mPlayer.release();
    mPlayer = null;
    }
    }

    }

    This is code for activity_audio_recorder.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".AudioRecorderActivity" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="126dp"
            android:text="@string/start_record" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button1"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="47dp"
            android:text="@string/start_play" />

    </RelativeLayout>

    Output Screen

    Android Audio Record

    Comments

    Popular posts from this blog

    SQLiteDatabase With Multiple Tables

    Programmatically turn ON/OFF WiFi on Android device

    Android Gestures