title: MediaRecorder start called in an invalid state: 0
body: <p>Brief description of the application:</p>
  
  <p>User will click the start button on the MainActivity.java that will direct to FirstWord.java. On FirstWord.java, the user can record themselves saying a word by clicking on the record button. There is a text to speech instance that will output the pronunciation of a word. </p>
  
  <p>The issues arises with the MediaRecorder. The application crashes once the user clicks on the recordButton. </p>
  
  <p>FirstWord.java</p>
  
  <p>The text to speech instance works properly, but I posted everything incase the textToSpeech is causing the MediaRecorder to fail. </p>
  
  <pre><code>package com.example.learnwords;
  
  import java.io.IOException;
  import java.util.Locale;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.media.MediaPlayer;
  import android.media.MediaRecorder;
  import android.os.Bundle;
  import android.os.Environment;
  import android.speech.tts.TextToSpeech;
  import android.speech.tts.TextToSpeech.OnInitListener;
  import android.view.Menu;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class FirstWord extends Activity implements OnInitListener {
  
       private TextToSpeech myTTS;
       private int MY_DATA_CHECK_CODE = 0;
  
          private static MediaRecorder mediaRecorder = new MediaRecorder();
          private static MediaPlayer mediaPlayer = new MediaPlayer();
          private static String audioFilePath;
          private static Button stopButton;
          private static Button playButton;
          private static Button recordButton;
  
          private boolean isRecording = false;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_first_word);   
  
          Intent checkTTSIntent = new Intent();
          checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
          startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
  
          recordButton = (Button) findViewById(R.id.recordButton);
          playButton = (Button) findViewById(R.id.playButton);
          stopButton = (Button) findViewById(R.id.stopButton);
  
          if (!hasMicrophone()) //calling hasMicrophone
          {
              stopButton.setEnabled(false);
              playButton.setEnabled(false);
              recordButton.setEnabled(false);
          } else {
              playButton.setEnabled(false);
              stopButton.setEnabled(false);
          }
  
          audioFilePath = 
               Environment.getExternalStorageDirectory().getAbsolutePath() 
                   + "/myaudio.3gp";  
      }
      protected boolean hasMicrophone() {
          PackageManager pmanager = this.getPackageManager();
          return pmanager.hasSystemFeature(
                 PackageManager.FEATURE_MICROPHONE);
      }
  
      public void recordAudio(View view)throws Exception{
          isRecording = true;
          stopButton.setEnabled(true);
          playButton.setEnabled(false);
          recordButton.setEnabled(false);
          try {
              mediaRecorder = new MediaRecorder();
              mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
              mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
              mediaRecorder.setOutputFile(audioFilePath);
              mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
              mediaRecorder.prepare();
            } catch (Exception e) {
             e.printStackTrace();
            }
          mediaRecorder.start();
  }
      public void stopClicked (View view)
      {   
          stopButton.setEnabled(false);
          playButton.setEnabled(true);
  
          if (isRecording)
          {   
              recordButton.setEnabled(false);
              mediaRecorder.stop();
              mediaRecorder.release();
              mediaRecorder = null;
              isRecording = false;
          } else {
              mediaPlayer.release();
                  mediaPlayer = null;
              recordButton.setEnabled(true);
          }
      }
      public void playAudio (View view) throws IOException
      {
          playButton.setEnabled(false);
          recordButton.setEnabled(false);
          stopButton.setEnabled(true);
  
          mediaPlayer = new MediaPlayer();
          mediaPlayer.setDataSource(audioFilePath);
          mediaPlayer.prepare();
          mediaPlayer.start();
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      private void speakWords(String speech) {
          //speak straight away
          myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
  }
  
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          if (requestCode == MY_DATA_CHECK_CODE) {
              if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                  //the user has the necessary data - create the TTS
                  myTTS = new TextToSpeech(this, this);
  
              }
              else {
                      //no data - install it now
                  Intent installTTSIntent = new Intent();
                  installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                  startActivity(installTTSIntent);
              }
          }
      }
          //setup TTS
      public void onInit(int initStatus) {
              //check for successful instantiation
          if (initStatus == TextToSpeech.SUCCESS) {
              if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
                  myTTS.setLanguage(Locale.US);
          }
          else if (initStatus == TextToSpeech.ERROR) {
              Toast.makeText(this, "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
          }
      }
          public void proClicked(View view) {
              TextView textView = (TextView)findViewById(R.id.textView1);
              String word = textView.getText().toString();
              speakWords(word);
  
          }
  }
  </code></pre>
  
  <p>LogCat</p>
  
  <pre><code>09-19 11:17:20.229: D/ActivityThread(4603): setTargetHeapUtilization:0.25
  09-19 11:17:20.229: D/ActivityThread(4603): setTargetHeapIdealFree:8388608
  09-19 11:17:20.229: D/ActivityThread(4603): setTargetHeapConcurrentStart:2097152
  09-19 11:17:20.680: I/Adreno200-EGLSUB(4603): &lt;ConfigWindowMatch:2087&gt;: Format RGBA_8888.
  09-19 11:17:20.720: E/(4603): &lt;s3dReadConfigFile:75&gt;: Can't open file for reading
  09-19 11:17:20.720: E/(4603): &lt;s3dReadConfigFile:75&gt;: Can't open file for reading
  09-19 11:17:22.061: V/MediaPlayer(4603): constructor
  09-19 11:17:22.061: V/MediaPlayer(4603): setListener
  09-19 11:17:22.241: I/Adreno200-EGLSUB(4603): &lt;ConfigWindowMatch:2087&gt;: Format RGBA_8888.
  09-19 11:17:24.363: I/TextToSpeech(4603): Sucessfully bound to com.google.android.tts
  09-19 11:17:24.373: E/SpannableStringBuilder(4603): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
  09-19 11:17:24.373: E/SpannableStringBuilder(4603): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
  09-19 11:17:24.373: I/TextToSpeech(4603): Connected to ComponentInfo{com.google.android.tts/com.google.android.tts.GoogleTTSService}
  09-19 11:17:28.998: W/System.err(4603): java.lang.RuntimeException: setAudioSource failed.
  09-19 11:17:29.018: W/System.err(4603):     at android.media.MediaRecorder._setAudioSource(Native Method)
  09-19 11:17:29.018: W/System.err(4603):     at android.media.MediaRecorder.setAudioSource(MediaRecorder.java:330)
  09-19 11:17:29.018: W/System.err(4603):     at com.example.learnwords.FirstWord.recordAudio(FirstWord.java:75)
  09-19 11:17:29.018: W/System.err(4603):     at java.lang.reflect.Method.invokeNative(Native Method)
  09-19 11:17:29.018: W/System.err(4603):     at java.lang.reflect.Method.invoke(Method.java:511)
  09-19 11:17:29.018: W/System.err(4603):     at android.view.View$1.onClick(View.java:3674)
  09-19 11:17:29.018: W/System.err(4603):     at android.view.View.performClick(View.java:4203)
  09-19 11:17:29.018: W/System.err(4603):     at android.view.View$PerformClick.run(View.java:17189)
  09-19 11:17:29.018: W/System.err(4603):     at android.os.Handler.handleCallback(Handler.java:615)
  09-19 11:17:29.018: W/System.err(4603):     at android.os.Handler.dispatchMessage(Handler.java:92)
  09-19 11:17:29.018: W/System.err(4603):     at android.os.Looper.loop(Looper.java:137)
  09-19 11:17:29.018: W/System.err(4603):     at android.app.ActivityThread.main(ActivityThread.java:4950)
  09-19 11:17:29.018: W/System.err(4603):     at java.lang.reflect.Method.invokeNative(Native Method)
  09-19 11:17:29.018: W/System.err(4603):     at java.lang.reflect.Method.invoke(Method.java:511)
  09-19 11:17:29.028: W/System.err(4603):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
  09-19 11:17:29.028: W/System.err(4603):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
  09-19 11:17:29.028: W/System.err(4603):     at dalvik.system.NativeStart.main(Native Method)
  09-19 11:17:29.028: E/MediaRecorder(4603): start called in an invalid state: 0
  09-19 11:17:29.028: W/dalvikvm(4603): threadid=1: thread exiting with uncaught exception (group=0x4196f438)
  09-19 11:17:29.038: E/AndroidRuntime(4603): FATAL EXCEPTION: main
  09-19 11:17:29.038: E/AndroidRuntime(4603): java.lang.IllegalStateException: Could not execute method of the activity
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.view.View$1.onClick(View.java:3679)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.view.View.performClick(View.java:4203)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.view.View$PerformClick.run(View.java:17189)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.os.Handler.handleCallback(Handler.java:615)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.os.Handler.dispatchMessage(Handler.java:92)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.os.Looper.loop(Looper.java:137)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.app.ActivityThread.main(ActivityThread.java:4950)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at java.lang.reflect.Method.invokeNative(Native Method)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at java.lang.reflect.Method.invoke(Method.java:511)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at dalvik.system.NativeStart.main(Native Method)
  09-19 11:17:29.038: E/AndroidRuntime(4603): Caused by: java.lang.reflect.InvocationTargetException
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at java.lang.reflect.Method.invokeNative(Native Method)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at java.lang.reflect.Method.invoke(Method.java:511)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.view.View$1.onClick(View.java:3674)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     ... 11 more
  09-19 11:17:29.038: E/AndroidRuntime(4603): Caused by: java.lang.IllegalStateException
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at android.media.MediaRecorder.start(Native Method)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     at com.example.learnwords.FirstWord.recordAudio(FirstWord.java:83)
  09-19 11:17:29.038: E/AndroidRuntime(4603):     ... 14 more
  </code></pre>
  
  <p>AndroidManifest.XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.learnwords"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.learnwords.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name="com.example.learnwords.FirstWord"
              android:label="@string/title_activity_first_word"
              android:parentActivityName="com.example.learnwords.MainActivity" &gt;
              &lt;meta-data
                  android:name="android.support.PARENT_ACTIVITY"
                  android:value="com.example.myfirstapp.MainActivity" /&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>I think those are the relevant files. If other files are needed, please comment. </p>
  
  <p>Any Ideas? Thanks.</p>