Kindle Fire Audio Recording and Playback using MediaPlayer and MediaRecorder

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Video Recording and Image Capture on the Kindle Fire using Camera IntentsWorking with the Amazon Maps API on the Kindle Fire


<google>BUY_KINDLE_FIRE</google>


Whilst the first and second generation Kindle Fire models both feature audio playback capabilities in the form of built-in speakers and a headphone socket, it was not until the introduction of the HD generation of devices that the ability to record audio was added. The same holds true of the ability to record video since a built in camera was not introduced until the HD Kindle Fire generation.

This chapter will provide an overview of the MediaRecorder class and explain the basics of how this class can be used to record audio or video. The use of the MediaPlayer class to play back audio will also be covered. Having covered the basics, an example application will be created to demonstrate these techniques in action. In addition to looking at audio and video handling, this chapter will also touch on the subjects of saving files to the SD card and the steps involved in detecting whether or not a device has a microphone or camera.


Contents


Playing Audio

In terms of audio playback, the Kindle Fire implementation of Android supports AAC LC/LTP, HE-AACv1 (AAC+), HE-AACv2 (enhanced AAC+), AMR-NB, AMR-WB, MP3, MIDI, Ogg Vorbis, and PCM/WAVE formats.

Audio playback can be performed using either the MediaPlayer or the AudioTrack classes. AudioTrack is a more advanced option that uses streaming audio buffers and provides greater control over the audio. The MediaPlayer class, on the other hand, provides an easier programming interface for implementing audio playback and will meet the needs of most audio requirements.

The MediaPlayer class has associated with it a range of methods that can be called by an application to perform certain tasks. A subset of some of the key methods of this class is as follows:

  • create() – Called to create a new instance of the class, passing through the Uri of the audio to be played.
  • setDataSource() – Sets the source from which the audio is to play.
  • prepare() – Instructs the player to prepare to begin playback.
  • start() – Starts the playback.
  • pause() – Pauses the playback. Playback may be resumed via a call to the resume() method.
  • stop() – Stops playback.
  • setVolume() – Takes two floating-point arguments specifying the playback volume for the left and right channels.
  • resume() – Resumes a previously paused playback session.
  • reset() – Resets the state of the media player instance. Essentially sets the instance back to the uninitialized state. At a minimum, a reset player will need to have the data source set again and the prepare() method called.
  • release() – To be called when the player instance is no longer needed. This method ensures that any resources held by the player are released.

In a typical implementation, an application will instantiate an instance of the MediaPlayer class, set the source of the audio to be played and then call prepare() followed by start(). For example:

MediaPlayer mediaPlayer = new MediaPlayer();
		
mediaPlayer.setDataSource("http://www.ebookfrenzy.com/myaudio.mp3");
mediaPlayer.prepare();
mediaPlayer.start();

Recording Audio and Video using the MediaRecorder Class

As with audio playback, recording can be performed using a number of different techniques. One option is to use the MediaRecorder class, which, as with the MediaPlayer class, provides a number of methods that are used to record audio:

  • setAudioSource() – Specifies the source of the audio to be recorded (typically this will be MediaRecorder.AudioSource.MIC for the device microphone).
  • setVideoSource() – Specifies the source of the video to be recorded (for example MediaRecorder.VideoSource.CAMERA).
  • setOutputFormat() – Specifies the format into which the recorded audio or video is to be stored (for example MediaRecorder.OutputFormat.AAC_ADTS).
  • setAudioEncoder() – Specifies the audio encoder to be used for the recorded audio (for example MediaRecorder.AudioEncoder.AAC).
  • setOutputFile() – Configures the path to the file into which the recorded audio or video is to be stored.
  • prepare() – Prepares the MediaRecorder instance to begin recording.
  • start() - Begins the recording process.
  • stop() – Stops the recording process. Once a recorder has been stopped, it will need to be completely reconfigured and prepared before being restarted.
  • reset() – Resets the recorder. The instance will need to be completely reconfigured and prepared before being restarted.
  • release() – Should be called when the recorder instance is no longer needed. This method ensures all resources held by the instance are released.

A typical implementation using this class will set the source, output and encoding format and output file. Calls will then be made to the prepare() and start() methods. The stop() method will then be called when recording is to end followed by the reset() method. When the application no longer needs the recorder instance a call to the release() method is recommended:

MediaRecorder mediaRecorder = new MediaRecorder();

mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setOutputFile(audioFilePath);
			
mediaRecorder.prepare();
mediaRecorder.start();
.
.
.
mediaRecorder.stop()
mediaRecorder.reset()
mediaRecorder.release()

In order to record audio, the manifest file for the application must include the android.permission.RECORD_AUDIO permission:

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

About the Example Project

The remainder of this chapter will work through the creation of an example application intended to demonstrate the use of the MediaPlayer and MediaRecorder classes to implement the recording and playback of audio on a Kindle Fire device.

When developing applications that make use of specific hardware features, the microphone being a case in point, it is important to check the availability of the feature before attempting to access it in the application code. This is of particular importance when developing for the Kindle Fire family since neither the first nor second-generation Kindle Fire models has a microphone. The application created in this chapter will, therefore, also demonstrate the steps involved in detecting the presence or otherwise of a microphone on the device.

Once completed, this application will provide a very simple interface intended to allow the user to record and playback audio. The recorded audio will need to be stored within an audio file on the device. That being the case, this tutorial will also briefly explore the mechanism for using SD Card storage.

Creating the AudioApp Project

Launch Eclipse and create a new Android Application project named AudioApp using the appropriate Android SDK versions and package name and including a blank activity named AudioAppActivity with a corresponding layout named activity_audio_app.

Designing the User Interface

Once the new project has been created, Eclipse should display the user interface layout associated with the activity_audio_app.xml file. Within the Graphical Layout tool, select the “Hello world!” TextView and delete it from the layout. Drag and drop three Button views onto the layout. The positioning of the buttons is not of paramount importance to this example, though Figure 39-1 shows a suggested layout.

Configure the buttons to display string resources that read Record, Play and Stop and give them view IDs of recordButton, playButton, and stopButton respectively.


The Ui for an example Android Audio app

Figure 39-1


Right-click on the Play button and select the Other Properties -> All by Name -> OnClick menu option to configure the button to call a method named playAudio when selected by the user. Repeat these steps to configure the remaining buttons to call methods named recordAudio and stopRecording respectively. Once the user interface design phase is complete, save the file.

Checking for Microphone Availability

Attempting to record audio on a device without a microphone will cause the Android system to throw an exception. It is vital, therefore, that the code check for the presence of a microphone before making such an attempt. There are a number of ways of doing this including checking for the physical presence of the device. An easier approach, and one that is more likely to work on different Android devices, is to ask the Android system if it has a package installed for a particular feature. This involves creating an instance of the Android PackageManager class and then making a call to the object’s hasSystemFeature() method. In this case, the feature of interest is PackageManager.FEATURE_MICROPHONE.

For the purposes of this example, we will create a method named hasMicrophone() that may be called upon to check for the presence of a microphone. Within the Package Explorer panel, locate and double click on the AudioAppActivity.java file and modify it to add this method:

package com.example.audioapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.view.Menu;

public class AudioAppActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_audio_app);
	}
	
	protected boolean hasMicrophone() {
		PackageManager pmanager = this.getPackageManager();
		return pmanager.hasSystemFeature(
               PackageManager.FEATURE_MICROPHONE);
	}
.
.
}

Performing the Activity Initialization

The next step is to modify the onCreate() method of the activity to perform a number of initialization tasks. Remaining within the AudioAppActivity.java file, modify the method as follows:

package com.example.audioapp;

import java.io.IOException;

import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.view.Menu;
import android.widget.Button;
import android.view.View;
import android.media.MediaPlayer;

public class AudioAppActivity extends Activity {

	private static MediaRecorder mediaRecorder;
	private static String audioFilePath;
	private static Button stopButton;
	private static Button playButton;
	private static Button recordButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_audio_app);
		
		recordButton = (Button) findViewById(R.id.recordButton);
		playButton = (Button) findViewById(R.id.playButton);
		stopButton = (Button) findViewById(R.id.stopButton);
	
		if (!hasMicrophone())
		{
			stopButton.setEnabled(false);
			playButton.setEnabled(false);
			recordButton.setEnabled(false);
		} else {
			playButton.setEnabled(false);
			stopButton.setEnabled(false);
		}
		
		audioFilePath = 
             Environment.getExternalStorageDirectory().getAbsolutePath() 
                 + "/myaudio.acc";
		
		mediaRecorder = new MediaRecorder();
	}
.
.
}

The added code begins by obtaining references to the three button views in the user interface. Next, the previously implemented hasMicrophone() method is called to ascertain whether the device includes a microphone. If it does not, all the buttons are disabled, otherwise only the Stop and Play buttons are disabled.

The next line of code needs a little more explanation:

audioFilePath = 
             Environment.getExternalStorageDirectory().getAbsolutePath() 
                 + "/myaudio.acc";

The purpose of this code is to identify the location of the SD card storage on the device and to use that to create a path to a file named myaudio.acc into which the audio recording will be stored. The path of the SD card (which is referred to as external storage even though it is internal to the device on the Kindle Fire) is obtained via a call to the getExternalStorageDirectory() method of the Android Environment class. Having obtained this path, the file name is then appended to the end of it. When executed on a Kindle Fire device, this results in the following path to the audio file:

/mnt/sdcard/myaudio.acc

When working with external storage it is important to be aware that such activity by an application requires permission to be requested in the application manifest file. For example:

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

Finally, the method creates a new instance of the MediaRecorder class.

Implementing the recordAudio() Method

When the user touches the Record button, the recordAudio() method will be called. This method will need to enable and disable the appropriate buttons, configure the MediaRecorder instance with information about the source of the audio, the output format and encoding and the location of the file into which the audio is to be stored. Finally, the prepare() and start() methods of the MediaRecorder object will need to be called. Combined, these requirements result in the following method implementation in the AudioAppActivity.java file:

public void recordAudio (View view) throws IOException
{
   stopButton.setEnabled(true);
   playButton.setEnabled(false);
   recordButton.setEnabled(false);

   try {		mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
	mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
	mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
	mediaRecorder.setOutputFile(audioFilePath);		
	mediaRecorder.prepare();
	mediaRecorder.start();
   } catch (Exception e) {
		e.printStackTrace();
   }		
}

Implementing the stopRecording() Method

The stopRecording() method is responsible for enabling the Play button, disabling the Stop button and then stopping and resetting the MediaRecorder instance. The code to achieve this reads as outlined in the following listing and should be added to the AudioAppAcitivy.java file:

public void stopRecording (View view)
{
	stopButton.setEnabled(false);
	playButton.setEnabled(true);
	recordButton.setEnabled(true);
		
	if (mediaRecorder != null)
	{
		mediaRecorder.stop();
		mediaRecorder.reset();
	}
}

Implementing the playAudio() method

The playAudio() method will simply create a new MediaPlayer instance, assign the audio file located on the SD card as the data source and then prepare and start the playback:

public void playAudio (View view) throws IOException
{
	MediaPlayer mediaPlayer = new MediaPlayer();
	mediaPlayer.setDataSource(audioFilePath);
	mediaPlayer.prepare();
	mediaPlayer.start();
}

Configuring Permissions in the Manifest File

Before testing the application, it is essential that the appropriate permissions be requested within the manifest file for the application. Specifically, the application will require permission to record audio and to access the external storage (SD card). Within the Package Explorer panel, locate and double click on the AndroidManifest.xml file to load it into the editor. Select the AndroidManifest.xml tab along the bottom edge of the editing panel to directly edit the XML and add the two permission tags:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.audioapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission 
            android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.audioapp.AudioAppActivity"
            android:label="@string/app_name" >
            <intent-filter>
              <action android:name="android.intent.action.MAIN" />

              <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Testing the Application

Compile and run the application on a Kindle Fire HD generation device and touch the Record button. After recording, touch Stop followed by Play, at which point the recorded audio should play back through the device speakers.

Summary

The Android SDK provides a number of mechanisms for the implementation of audio recording and playback. This chapter has looked at two of these in the form of the MediaPlayer and MediaRecorder classes. Having covered the theory of using these techniques, this chapter worked through the creation of an example application designed to record and then play back audio. In the course of working with audio in Android, this chapter also looked at the steps involved in ensuring that the device on which the application is running has a microphone before attempting to record audio. The use of external storage in the form of the built-in Kindle Fire SD card was also covered.


<google>BUY_KINDLE_FIRE</google>



PreviousTable of ContentsNext
Video Recording and Image Capture on the Kindle Fire using Camera IntentsWorking with the Amazon Maps API on the Kindle Fire