How to handle Audio Recording and Playing in Android

This article describes how we can use audio API to record sound and to play sound. This gives option also include time (how long it is going to record sound).

First of all we need to decide how we get notification when some events happen in audio system. Events mean some signal, for example your code need to be notified when audio play has started or stopped. Your class which one wants to be notified, needs to implement some java interface. Interface is a contact between two objects. Who want to get notification, it should implements the interface methods.
Here are the methods we specify for our need.


package com.hemelix.audiotest;

/*
 *  interface InterfaceAudioEvents, the class who want to get notify about audio event must implement this interface.
 *  Like MainActivity class (our UI class) want to be notified  and MainActivity has implemented InterfaceAudioEvents interface and it's methods
 *  (c) Hemelix, more tutorial at hemelix.com
 */
public interface InterfaceAudioEvents {
	/*
	 * Called when recording just started.
	 */
	void audioRecordStarted();
	/*
	 * Called when recording done.
	 */
	void audioRecordDone();
	/*
	 * Called when the recorded audio has just started.
	 */
	void audioPlayeStarted();
	/*
	 * Called when the recorded audio has been played .
	 */
	void audioPlayeDone();
}

Now the main activity class is looks like this

public class MainActivity extends Activity implements InterfaceAudioEvents {
..
..
	@Override
	public void audioRecordDone() {
		startBtn.setEnabled(true);
	}

	@Override
	public void audioRecordStarted() {
		startBtn.setEnabled(false);
	}

	@Override
	public void audioPlayeDone() {
		startBtn.setEnabled(true);
	}

	@Override
	public void audioPlayeStarted() {
		startBtn.setEnabled(false);
	}

Now you see we have implemented all the four methods in our class. When any of these events occur then Audio system class will call these method. Thus we can change our UI button and so on.
Now the biggest question how we make a link between the activity class and the audio class. We shall explain these mechanism below. Look at the constructor of the HandleAudioPlayerRecorder class. It has Context parameter and InterfaceAudioEvents parameter. When we create the HandleAudioPlayerRecorder class then we pass a reference to a type of object InterfaceAudioEvents. Thus when any event occurs then the right method can be called.

public class HandleAudioPlayerRecorder {
	private static final String TAG = "HandleAudioPlayerRecorder";
        ...
	/*
	 * HandleAudioPlayerRecorder, Constructor, during construction time it sets the private file where we can record the audio
	 * @param aCtx, Context of the application, need to find the private path of the application
	 * @param aReceiver, need to call back when audio related events happen
	 */
	public HandleAudioPlayerRecorder(Context aCtx,
		InterfaceAudioEvents aReceiver) {
		this.mReceiver = aReceiver;
		this.mCtx = aCtx;
...

Look how we create an instance of HandleAudioPlayerRecorder class. We are passing this and this references. First this is for the Context and second this is for InterfaceAudioEvents.

mAudioHandler = new HandleAudioPlayerRecorder(this, this);

You can download the source code to eclipse and try it with your android phone.

Download Source Code Example