title: Media recorder stops in an invalid state
body: <p>I'm attempting to create a simple app that records audio. I'm using the standard <a href="https://developer.android.com/reference/android/media/MediaRecorder" rel="nofollow noreferrer">MediaRecorder</a> to achieve that. Here is what I have so far:</p>
  
  <ol>
  <li><p>I'm successfully starting the mediaRecorder using this method</p>
  
  <pre><code>public void startRecording(String filePath) throws IOException {
  
  String state = android.os.Environment.getExternalStorageState();
  if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
      throw new IOException("SD Card is not mounted.  It is " + state + ".");
  }
  
  // make sure the directory we plan to store the recording in exists
  File directory = new File(filePath).getParentFile();
  if (!directory.exists() &amp;&amp; !directory.mkdirs()) {
      throw new IOException("Path to file could not be created.");
  }
  
  recorder.setAudioSource(MediaRecorder.AudioSource.MIC );
  
  recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  recorder.setOutputFile(filePath);
  
  recorder.setOnErrorListener(errorListener);
  recorder.setOnInfoListener(infoListener);
  
  try {
      recorder.prepare();
  
  } catch (Exception e) {
      Timber.e("Error while prepareing media recorder " + e.getMessage());
  }
  
  try {
  
      recorder.start();
      SharedPrefs.setIsRecording(true);
      Timber.d("Started to record " + SharedPrefs.isRecording());
  
  } catch (Exception e) {
      Timber.e("Error while starting the media recorder " + e.getMessage());
  } }
  </code></pre></li>
  </ol>
  
  <p>this works fine and I can see locally that the file is created in the specified folder.</p>
  
  <ol start="2">
  <li><p>Now this is what I use to stop recording:</p>
  
  <pre><code>public void stopRecording() {
  
  if (recorder == null) {
          Timber.e("recorder is null");
      }
  
  if (SharedPrefs.isRecording()) {
  
      try {
          recorder.stop();
  
      } catch (Exception r) {
          Timber.e("Error while attempting to stop " + r.getMessage());
      }
  
      try {
          recorder.release();
  
      } catch (Exception e) {
          Timber.e("Error while attempting to release " + e.getMessage());
      }
  
      Timber.e("We stopped recording");
      SharedPrefs.setIsRecording(false);
  
  } else {
      Timber.e("Attempting to stop recording while not recording ");
  } }
  </code></pre>
  
  <p>Now when stopRecording() is being called this is the error message that I get:</p>
  
  <pre><code> E/MediaRecorder: stop called in an invalid state:1
  </code></pre>
  
  <p>AudioRecorder: Error while attempting to stop null</p></li>
  </ol>
  
  <p>Note: When I locally go to the folder where the file is and I refresh it, I can see the size of the file is incrementing.</p>
  
  <p>Thanks in advance!</p>