package course.examples.audiovideo.audiorecording;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

public class AudioRecordingActivity extends Activity {
	private static final String TAG = "AudioRecordTest";
	private static final String mFileName = Environment
			.getExternalStorageDirectory().getAbsolutePath()
			+ "/audiorecordtest.3gp";
	private MediaRecorder mRecorder;
	private MediaPlayer mPlayer;
	private AudioManager mAudioManager;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);

		final ToggleButton mRecordButton = (ToggleButton) findViewById(R.id.record_button);
		final ToggleButton mPlayButton = (ToggleButton) findViewById(R.id.play_button);

		// Set up record Button
		mRecordButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {

				// Set checked state
				mRecordButton.setEnabled(!isChecked);

				// Start/stop recording
				onRecordPressed(isChecked);

			}
		});

		// Set up play Button
		mPlayButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {

				// Set checked state
				mRecordButton.setEnabled(!isChecked);

				// Start/stop playback
				onPlayPressed(isChecked);
			}
		});

		// Get AudioManager
		mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

		// Request audio focus
		mAudioManager.requestAudioFocus(afChangeListener,
				AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

	}

	// Toggle recording
	private void onRecordPressed(boolean shouldStartRecording) {

		if (shouldStartRecording) {
			startRecording();
		} else {
			stopRecording();
		}

	}

	// Start recording with MediaRecorder
	private void startRecording() {

		mRecorder = new MediaRecorder();
		mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		mRecorder.setOutputFile(mFileName);
		mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

		try {
			mRecorder.prepare();
		} catch (IOException e) {
			Log.e(TAG, "Couldn't prepare and start MediaRecorder");
		}

		mRecorder.start();
	}

	// Stop recording. Release resources
	private void stopRecording() {

		if (null != mRecorder) {
			mRecorder.stop();
			mRecorder.release();
			mRecorder = null;
		}

	}

	// Toggle playback
	private void onPlayPressed(boolean shouldStartPlaying) {

		if (shouldStartPlaying) {
			startPlaying();
		} else {
			stopPlaying();
		}

	}

	// Playback audio using MediaPlayer
	private void startPlaying() {

		mPlayer = new MediaPlayer();
		try {
			mPlayer.setDataSource(mFileName);
			mPlayer.prepare();
			mPlayer.start();
		} catch (IOException e) {
			Log.e(TAG, "Couldn't prepare and start MediaPlayer");
		}

	}

	// Stop playback. Release resources
	private void stopPlaying() {

		if (null != mPlayer) {
			if (mPlayer.isPlaying())
				mPlayer.stop();
			mPlayer.release();
			mPlayer = null;
		}

	}

	// Listen for Audio Focus changes
	OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {

		@Override
		public void onAudioFocusChange(int focusChange) {

			if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
				mAudioManager.abandonAudioFocus(afChangeListener);

				// Stop playback, if necessary
				if (mPlayer.isPlaying())
					stopPlaying();
			}

		}

	};

	// Release recording and playback resources, if necessary
	@Override
	public void onPause() {
		super.onPause();

		if (null != mRecorder) {
			mRecorder.release();
			mRecorder = null;
		}

		if (null != mPlayer) {
			mPlayer.release();
			mPlayer = null;
		}

	}

}
@@ -1349,37 +1349,37 @@
 ) %7B%0A%0A%09%09%09%09// Set 
-check
+enabl
 ed state%0A%09%09%09%09mRe
@@ -1368,38 +1368,36 @@
 bled state%0A%09%09%09%09m
-Record
+Play
 Button.setEnable
@@ -1697,13 +1697,13 @@
 Set 
-check
+enabl
 ed s
@@ -3398,33 +3398,32 @@
 stopPlaying() %7B%0A
-%0A
 %09%09if (null != mP
@@ -3516,33 +3516,32 @@
 yer = null;%0A%09%09%7D%0A
-%0A
 %09%7D%0A%0A%09// Listen f
@@ -3860,24 +3860,43 @@
 ary%0A%09%09%09%09if (
+null != mPlayer && 
 mPlayer.isPl

fix toggling bug