Audio Capture with an Intent

The easiest way to simply allow audio recording capabilities in an application is to leverage an existing application through an intent that provides recording capabilities. In the case of audio, Android ships with a sound recorder application that can be triggered in this manner.

The action used to create the intent is available as a constant called RECORD_SOUND_ACTION within the MediaStore.Audio.Media class. Here is the basic code to trigger the built-in sound recorder.

Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); startActivity(intent);

Figure 7-1 shows the built-in audio recording application as triggered by an intent.

Record Sound Action
Figure 7-1. Android's built-in sound recorder triggered with an intent

Of course, in order to retrieve the recording that the user creates, we'll want to use startActivityForResult rather than just startActivity.

Here is an example that triggers the built-in sound recorder via an intent. When the user finishes, the audio file is played back using the MediaPlayer.

package com.apress.proandroidmedia.ch07.intentaudiorecord;

import; import android.content.Intent; import;



import android.os.Bundle;

import android.provider.MediaStore;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

Our activity implements OnClickListener so that it can respond to Button presses, and OnCompletionListener so that it can be notified when audio is finished playing in the MediaPlayer.

public class IntentAudioRecorder extends Activity implements OnClickListener,~ OnCompletionListener {

We'll create a constant called RECORD_REQUEST that we can pass into the startActivityForResult function so that we can identify the source of any calls to onActivityResult, which is called when the sound recorder is complete.

The onActivityResult method would be called by any returning activity when triggered by a startActivityForResult function. Passing in a unique constant along with the intent allows us to differentiate between them within the onActivityResult method.

public static int RECORD_REOUEST = 0; Button createRecording, playRecording;

We need a Uri object that we will use to contain the Uri to the audio file that is recorded by the sound recorder activity.

Uri audioFileUri; ^Override public void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState); setContentView(R.layout.main);

After we set the content view, we can obtain references to the Button objects. Each one's click listener is set to this so that our activity's onClick method will be called. Also, we'll set the playRecording button to not be enabled until we have an audio file to play (get a result from the sound recorder activity).

createRecording = (Button) this.findViewByld(; createRecording.setOnClickListener(this);

playRecording = (Button) this.findViewByld(;



When we click either of the Buttons, our onClick method is triggered. If the createRecording Button was clicked, we trigger the sound recorder activity through the MediaStore.Audio.Media.RECORD_SOUND_ACTION action in an intent passed into startActivityForResult.

public void onClick(View v) { if (v == createRecording) { Intent intent =

new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); startActivityForResult(intent, RECORD_REOUEST); } else if (v == playRecording) {

If the playRecording Button was pressed, we create a MediaPlayer and set it to play the audio file represented by the Uri that was returned from the sound recorder activity and saved in our audioFileUri object.

We also set the OnCompletionListener of the MediaPlayer to be ourselves so that our OnCompletion method is called when it is done playing, and we disable the playRecording Button so that the user cannot trigger the playback to happen again until we are ready.

MediaPlayer mediaPlayer = MediaPlayer.create(this, audioFileUri);




Our onActivityResult method is triggered when the sound recorder activity is complete. The resultCode should equal the RESULT_OK constant, and the requestCode should equal the value we passed into the startActivityForResult method, RECORD_REQUEST. If both of those are true, then we can retrieve the Uri of the recorded audio file by getting it out of the intent that is passed back to us through its getData method. Once all of that is done, we enable the playRecording Button so that we can play the returned audio file.

protected void onActivityResult (int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == RECORD_REOUEST) { audioFileUri = data.getData(); playRecording.setEnabled(true);

Finally, in our onCompletion method, which is called when the MediaPlayer is done playing a file, we re-enable the playRecording Button so the user may listen to the audio file again if he or she so chooses.

public void onCompletion(MediaPlayer mp) { playRecording.setEnabled(true);

Here is the layout XML that is in use by our activity. <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<Button android:text="Record Audio" android:id="@+id/RecordButton"^ android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="Play Recording" android:id="@+id/PlayButton"^ android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>

As you can see, simply adding audio recording capabilities is straightforward. It doesn't allow much in the way of control over the user interface or other aspects of the recording, but it does give a no-frills usable interface to the user without much work on our part.

+4 -2


  • mk
    This code did not work for my LG phone <br /><br />Error: No Activity found to handle Intent{ act = android.provider.MediaStore.RECORD_SOUND}
    6 years ago
  • Kathleen
    How to pass mediaplayer in intent to activity?
    6 years ago
  • pia
    How to play recorded audio using an Intent in android?
    5 years ago
  • eddie
    How to capture audio and respond to it in android?
    2 years ago
    How to start recording with intent?
    8 months ago

Post a comment