title: illegalStateException in android
body: <p>I taken sound recording code from 
  <a href="http://developer.android.com/guide/topics/media/index.html" rel="nofollow">http://developer.android.com/guide/topics/media/index.html</a>, after running it on my emulator it is throwing illegalStateException, I also tried in my device, but there also its same thing.</p>
  
  <p>I am new to android, please help me</p>
  
  <pre><code>package com.android.audiorecordtest;
  
  import android.app.Activity;
  import android.widget.LinearLayout;
  import android.os.Bundle;
  import android.os.Environment;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.content.Context;
  import android.util.Log;
  import android.media.MediaRecorder;
  import android.media.MediaPlayer;
  
  import java.io.IOException;
  
  
  public class AudioRecordTest extends Activity
  {
      private static final String LOG_TAG = "AudioRecordTest";
      private static String mFileName = null;
  
      private RecordButton mRecordButton = null;
      private MediaRecorder mRecorder = null;
  
      private PlayButton   mPlayButton = null;
      private MediaPlayer   mPlayer = null;
  
      private void onRecord(boolean start) {
          if (start) {
              startRecording();
          } else {
              stopRecording();
          }
      }
  
      private void onPlay(boolean start) {
          if (start) {
              startPlaying();
          } else {
              stopPlaying();
          }
      }
  
      private void startPlaying() {
          mPlayer = new MediaPlayer();
          try {
              mPlayer.setDataSource(mFileName);
              mPlayer.prepare();
              mPlayer.start();
          } catch (IOException e) {
              Log.e(LOG_TAG, "prepare() failed");
          }
      }
  
      private void stopPlaying() {
          mPlayer.release();
          mPlayer = null;
      }
  
      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(LOG_TAG, "prepare() failed");
          }
  
          mRecorder.start();
      }
  
      private void stopRecording() {
          mRecorder.stop();
          mRecorder.release();
          mRecorder = null;
      }
  
      class RecordButton extends Button {
          boolean mStartRecording = true;
  
          OnClickListener clicker = new OnClickListener() {
              public void onClick(View v) {
                  onRecord(mStartRecording);
                  if (mStartRecording) {
                      setText("Stop recording");
                  } else {
                      setText("Start recording");
                  }
                  mStartRecording = !mStartRecording;
              }
          };
  
          public RecordButton(Context ctx) {
              super(ctx);
              setText("Start recording");
              setOnClickListener(clicker);
          }
      }
  
      class PlayButton extends Button {
          boolean mStartPlaying = true;
  
          OnClickListener clicker = new OnClickListener() {
              public void onClick(View v) {
                  onPlay(mStartPlaying);
                  if (mStartPlaying) {
                      setText("Stop playing");
                  } else {
                      setText("Start playing");
                  }
                  mStartPlaying = !mStartPlaying;
              }
          };
  
          public PlayButton(Context ctx) {
              super(ctx);
              setText("Start playing");
              setOnClickListener(clicker);
          }
      }
  
      public AudioRecordTest() {
          mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
          mFileName += "/audiorecordtest.3gp";
      }
  
      @Override
      public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
  
          LinearLayout ll = new LinearLayout(this);
          mRecordButton = new RecordButton(this);
          ll.addView(mRecordButton,
              new LinearLayout.LayoutParams(
                  ViewGroup.LayoutParams.WRAP_CONTENT,
                  ViewGroup.LayoutParams.WRAP_CONTENT,
                  0));
          mPlayButton = new PlayButton(this);
          ll.addView(mPlayButton,
              new LinearLayout.LayoutParams(
                  ViewGroup.LayoutParams.WRAP_CONTENT,
                  ViewGroup.LayoutParams.WRAP_CONTENT,
                  0));
          setContentView(ll);
      }
  
      @Override
      public void onPause() {
          super.onPause();
          if (mRecorder != null) {
              mRecorder.release();
              mRecorder = null;
          }
  
          if (mPlayer != null) {
              mPlayer.release();
              mPlayer = null;
          }
      }
  }
  </code></pre>
  
  <p><strong>Stack Trace:</strong></p>
  
  <pre><code>&gt; 06-01 11:06:18.440:
  &gt; ERROR/AndroidRuntime(724): Uncaught
  &gt; handler: thread main exiting due to
  &gt; uncaught exception 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):
  &gt; java.lang.IllegalStateException 06-01
  &gt; 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; android.media.MediaRecorder.start(Native
  &gt; Method) 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; com.example.android.whereareyou.Main.startRecording(Main.java:75)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; com.example.android.whereareyou.Main.onRecord(Main.java:32)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; com.example.android.whereareyou.Main.access$0(Main.java:30)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; com.example.android.whereareyou.Main$RecordButton$1.onClick(Main.java:89)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; android.view.View.performClick(View.java:2179)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; android.view.View.onTouchEvent(View.java:3828)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; android.widget.TextView.onTouchEvent(TextView.java:6291)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; android.view.View.dispatchTouchEvent(View.java:3368)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1707)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1197)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; android.app.Activity.dispatchTouchEvent(Activity.java:1993)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1691)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; android.view.ViewRoot.handleMessage(ViewRoot.java:1525)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; android.os.Handler.dispatchMessage(Handler.java:99)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; android.os.Looper.loop(Looper.java:123)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; android.app.ActivityThread.main(ActivityThread.java:3948)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; java.lang.reflect.Method.invokeNative(Native
  &gt; Method) 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; java.lang.reflect.Method.invoke(Method.java:521)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
  &gt; 06-01 11:06:18.449:
  &gt; ERROR/AndroidRuntime(724):     at
  &gt; dalvik.system.NativeStart.main(Native
  &gt; Method)
  </code></pre>