Media Recorder Example

Here is the code for a full custom audio capture and playback example using the MediaRecorder class.

package com.apress.proandroidmedia.ch07.customrecorder;

import; import;





import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

Our CustomRecorder activity implements OnClickListener so that it may be notified when Buttons are pressed, and OnCompletionListener so that it can respond when MediaPlayer has completed playing audio.

public class CustomRecorder extends Activity implements OnClickListener,~ OnCompletionListener {

We'll have a series of user interface components. The first, a TextView called statusTextView, will report the status of the application to the user: "Recording," "Ready to Play," and so on.

TextView statusTextView;

A series of buttons will be used for controlling various aspects. The names of the Buttons describe their use.

Button startRecording, stopRecording, playRecording, finishButton;

We'll have a MediaRecorder for recording the audio and a MediaPlayer for playing it back.

MediaRecorder recorder; MediaPlayer player;

Finally, we have a File object called audioFile, which will reference the file that is recorded to.

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

When the activity starts up, we'll set the text of the statusTextView to be "Ready."

statusTextView = (TextView) this.findViewByld(; statusTextView.setText("Ready");

stopRecording = (Button) this.findViewByld(; startRecording = (Button) this.findViewByld(; playRecording = (Button) this.findViewByld(; finishButton = (Button) this.findViewByld(;

We'll set all of the Buttons' onClickListeners to be this so that our onClick method is called when any of them are pressed.

startRecording.setOnClickListener(this); stopRecording.setOnClickListener(this); playRecording.setOnClickListener(this); finishButton.setOnClickListener(this);

Finally, in the onCreate method, we'll disable the stopRecording and playRecording Buttons since they won't work until we either start recording or finish recording respectively.

stopRecording.setEnabled(false); playRecording.setEnabled(false);

In the following onClick method, we handle all of the Button presses.

public void onClick(View v) { if (v == finishButton) {

If the finishButton is pressed, we finish the activity.

If the stopRecording Button is pressed, we call stop and release on the MediaRecorder object.

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

We then construct a MediaPlayer object and have it prepare to play back the audio file that we just recorded.

player = new MediaPlayer(); player.setOnCompletionListener(this);

The following two methods that we are using on the MediaPlayer, setDataSource and prepare, may throw a variety of exceptions. In the following code, we are simply throwing them. In your application development, you will probably want to catch and deal with them more elegantly, such as alerting the user when a file doesn't exist.

player.setDataSource(audioFile.getAbsolutePath()); } catch (IllegalArgumentException e) { throw new RuntimeException(

"Illegal Argument to MediaPlayer.setDataSource", e); } catch (IllegalStateException e) { throw new RuntimeException(

"Illegal State in MediaPlayer.setDataSource", e); } catch (IOException e) {

throw new RuntimeException(

"IOException in MediaPalyer.setDataSource", e);

player.prepare(); } catch (IllegalStateException e) { throw new RuntimeException(

"IllegalStateException in MediaPlayer.prepare", e); } catch (IOException e) {

throw new RuntimeException("IOException in MediaPlayer.prepare", e);

We set the statusTextView to indicate to the user that we are ready to play the audio file.

statusTextView.setText("Ready to Play");

We then set the playRecording and startRecording Buttons to be enabled and disable the stopRecording Button, as we are not currently recording.

playRecording.setEnabled(true); stopRecording.setEnabled(false); startRecording.setEnabled(true); } else if (v == startRecording) {

When the startRecording Button is pressed, we construct a new MediaRecorder and call setAudioSource, setOutputFormat, and setAudioEncoder.

recorder = new MediaRecorder();




We then create a new File on the SD card and call setOutputFile on the MediaRecorder object.

File path = new File(Environment.getExternalStorageDirectory()^ .getAbsolutePath() + "/Android/data/com.apress.proandroidmedia.ch07w .customrecorder/files/");


audioFile = File.createTempFile("recording", ".3gp", path); } catch (IOException e) {

throw new RuntimeException("Couldn't create recording audio file",e);

recorder.setOutputFile(audioFile.getAbsolutePath()); We call prepare on the MediaRecorder and start to begin the recording.

recorder.prepare(); } catch (IllegalStateException e) { throw new RuntimeException(

"IllegalStateException on MediaRecorder.prepare", e); } catch (IOException e) {

throw new RuntimeException("IOException on MediaRecorder.prepare",e);


Last, we update the statusTextView and change which Buttons are enabled and disabled.


playRecording.setEnabled(false); stopRecording.setEnabled(true); startRecording.setEnabled(false); } else if (v == playRecording) {

The last Button that we need to respond to is playRecording. When the stopRecording Button is pressed, the MediaPlayer object, player, is constructed and configured. All that we need to do when the playRecording Button is pushed is to start the playback, set the status message, and change which Buttons are enabled.


statusTextView.setText("Playing"); playRecording.setEnabled(false); stopRecording.setEnabled(false); startRecording.setEnabled(false);

The onCompletion method is called when the MediaPlayer object has completed playback of a recording. We use it to change the status message and set which Buttons are enabled.

public void onCompletion(MediaPlayer mp) { playRecording.setEnabled(true); stopRecording.setEnabled(false); startRecording.setEnabled(true); statusTextView.setText("Ready");

Here is the layout XML file, main.xml, for the foregoing activity. <?xml version="1.0" encoding="utf-8"?>

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

android:layout_height="fill_parent" >

<TextView android:layout_width="wrap_content" android:layout_height=^ "wrap_content" android:id="@+id/StatusTextView" android:text="Status"^ android:textSize="35dip"></TextView>

<Button android:text="Start Recording" android:id="@+id/StartRecording"^ android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

<Button android:text="Stop Recording" android:id="@+id/StopRecording"^ android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

<Button android:text="Play Recording" android:id="@+id/PlayRecording"^ android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

<Button android:layout_width="wrap_content" android:layout_height="wrap_content"^ android:id="@+id/FinishButton" android:text="Finish"></Button> </LinearLayout>

We'll also need to add the following permissions to the AndroidManifest.xml file.

<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">^ </uses-permission>

As we have seen, developing a custom audio capture application using MediaRecorder is not too cumbersome. Now let's look at how we can use the MediaRecorder's other methods to add other features.

+1 0


  • gabriele schroeder
    How to start media recorder in oncreate method in android?
    5 years ago

Post a comment