title: Android MediaRecorder IllegalStateException
body: <p>I am making an application that allows a user to record audio and save it somewhere in the SD card. I am using a MediaRecorder to do record the audio.</p>
  
  <p>I am reusing some of the code from the androiddevblog website as it was recommended by another user on stackoverflow to check those tutorials.</p>
  
  <p>My problem is whenever I click the button to record audio I get an error saying "Your Application has been forced to stop". I used the debugger to find out that it is because I have an illegalStateException on recorder.stop()</p>
  
  <p>I am aware that nothing will get recorded based on my code. I want to first make sure the file gets created and saved.</p>
  
  <p>I posted this question before, and solved my original problem but ran into this one after. So it may seem as a duplicate. </p>
  
  <pre><code>public class MyRecorderActivity extends Activity{
  
  private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
   private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp"    
  private Button audio;
  
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.questionandanswer);
  ....
  ....
  
   audio = (Button) findViewById(R.id.audio_recordactivity);
      audio.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
  
          startRecording();
  
  
          }
      });
  
   } 
  
    private String getFilename(){
          String filepath = Environment.getExternalStorageDirectory().getPath();
          File file = new File(filepath,AUDIO_RECORDER_FOLDER);
  
          if(!file.exists()){
                  file.mkdirs();
          }
  
          return (file.getAbsolutePath()+ "/ " + System.currentTimeMillis() +  AUDIO_RECORDER_FILE_EXT_3GP);
  
    }
  
   private void startRecording(){
          MediaRecorder recorder = new MediaRecorder();
  
          recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
          recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
          recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
          recorder.setOutputFile(getFilename());
  
          try {
                  recorder.prepare();
                  recorder.start();
          } catch (IllegalStateException e) {
                  e.printStackTrace();
          } catch (IOException e) {
                  e.printStackTrace();
          }
  
          recorder.stop();
          recorder.reset();
          recorder.release();
     }
  
  }
  </code></pre>