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.

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.

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.