Exploring Audio Recording

The Android media framework supports recording audio and video. You record media using the android.media.MediaRecorder class. In this section, we'll show you how to build an application that records audio content and then plays the content back. (We discuss video recording in Chapter 12.) The user interface of the application is shown in Figure 9-6.

Figure 9-6. The user interface of the audio-recorder example

As shown in Figure 9-6, the application contains four buttons: two to control recording, and two to start and stop playback of the recorded content. Listing 9-5 shows the layout file and activity class for the UI.

Listing 9-5. Media Recording and Playback in Android // record.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<Button android:id="@+id/bgnBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Begin Recording"/>

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

<Button android:id= "@+id/playRecordingBtn" android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="Play Recording"/>

<Button android:id= "@+id/stpPlayingRecordingBtn" android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="Stop Playing Recording"/>

</LinearLayout> // RecorderActivity.java import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class RecorderActivity extends Activity { private MediaPlayer mediaPlayer; private MediaRecorder recorder;

private static final String OUTPUT_FILE= "/sdcard/recordoutput.3gpp"; ^Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);


Button startBtn = (Button) findViewById(R.id.bgnBtn);

Button endBtn = (Button) findViewById(R.id.stpBtn);

Button playRecordingBtn = (Button) findViewById(R.id.playRecordingBtn);

Button stpPlayingRecordingBtn = (Button) findViewById(R.id.stpPlayingRecordingBtn);

startBtn.setOnClickListener(new OnClickListener() {

^Override public void onClick(View view) { try {

beginRecording(); } catch (Exception e) { e.printStackTrace();

endBtn.setOnClickListener(new OnClickListener() { ^Override public void onClick(View view) { try {

stopRecording(); } catch (Exception e) { e.printStackTrace();

playRecordingBtn.setOnClickListener(new OnClickListener() { ^Override public void onClick(View view) { try {

playRecording(); } catch (Exception e) { e.printStackTrace();

stpPlayingRecordingBtn.setOnClickListener(new OnClickListener() {

^Override public void onClick(View view) { try {

stopPlayingRecording(); } catch (Exception e) { e.printStackTrace();

private void beginRecording() throws Exception { killMediaRecorder();

File outFile = new File(OUTPUT_FILE);


recorder = new MediaRecorder();







private void stopRecording() throws Exception { if (recorder != null) { recorder.stop();

private void killMediaRecorder() { if (recorder != null) { recorder.release();

private void killMediaPlayer() { if (mediaPlayer != null) { try {

mediaPlayer.release(); } catch (Exception e) { e.printStackTrace();

private void playRecording() throws Exception { killMediaPlayer();

mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource(OUTPUT_FILE);

mediaPlayer.prepare(); mediaPlayer.start();

private void stopPlayingRecording() throws Exception {

if(mediaPlayer!=null) {


^Override protected void onDestroy() { super.onDestroy();

killMediaRecorder(); killMediaPlayer();

Before we jump into to Listing 9-5, realize that in order to record audio, you'll need to add the following permission to your manifest file:

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

If you look at the onCreate() method in Listing 9-5, you see that the on-click event handlers are wired up for the four buttons. The beginRecording() method handles recording. To record audio, you must create an instance of MediaRecorder and set the audio source, output format, audio encoder, and output file. At this point, the only supported audio source is the microphone, and you must set the encoder to AMR_NB, which signifies the Adaptive Multi-Rate

(AMR) narrowband audio codec. The only supported output format for audio is 3rd Generation Partnership Project (3GPP). The recorded audio is written to the SD card at /sdcard/ recordoutput.3gpp. Note that Listing 9-5 assumes that you've created an SD-card image and that you've pointed the emulator to the SD card. If you have not done this, refer to the section "Video Playback from the SD Card" for details on setting this up.

Note that the current media APIs do not support streaming. For example, if you record audio, you cannot access the audio stream during the recording process (for analysis purposes, for example). Instead, you have to write the audio content to a file first and then work with it. Future releases of the Android SDK will likely support audio streaming. Finally, as we mentioned earlier, version 1.0 of the Android SDK does not support video recording. This feature will also probably be supported in a later release.

This concludes our discussion of the media APIs. We're sure you'll agree that playing media content is quite simple with Android. The MediaPlayer class and VideoView control wrap things up nicely. Recording audio is also simple. For more on the media framework, see Chapter 12.

Now we'll move on to the telephony APIs.

0 0

Post a comment