title: Android MediaRecorder is not working and crashes on stop() method
body: <p>I'm learning android development and i'm using MediaRecorder class for recording audio. And the logcat says that the app crashes in the stop() method and it throws IllegalStateException. I don't understand why it crashes, and i call the method stop before start.</p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  private MediaRecorder grabacion;
  private String archivoSalida = null;
  private Button btn_recorder;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      btn_recorder = (Button)findViewById(R.id.btn_rec);
  
      if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
          ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO,}, 1000);
      }
  }
  
  public void Recorder(View view){
      if(grabacion == null){
          archivoSalida = Environment.getExternalStorageDirectory().getAbsolutePath() + &quot;/Grabacion.mp3&quot;;
          grabacion = new MediaRecorder();
          grabacion.setAudioSource(MediaRecorder.AudioSource.MIC);
          grabacion.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
          grabacion.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
          grabacion.setOutputFile(archivoSalida);
  
          try{
              grabacion.prepare();
              grabacion.start();
          } catch (IOException e){
          }
  
          btn_recorder.setBackgroundResource(R.drawable.rec);
          Toast.makeText(getApplicationContext(), &quot;Grabando...&quot;, Toast.LENGTH_SHORT).show();
      } else if(grabacion != null){
          grabacion.stop();
          grabacion.release();
          grabacion = null;
          btn_recorder.setBackgroundResource(R.drawable.stop_rec);
          Toast.makeText(getApplicationContext(), &quot;Grabación finalizada&quot;, Toast.LENGTH_SHORT).show();
      }
  }
  
  public void reproducir(View view) {
  
      MediaPlayer mediaPlayer = new MediaPlayer();
      try {
          mediaPlayer.setDataSource(archivoSalida);
          mediaPlayer.prepare();
      } catch (IOException e){
      }
  
      mediaPlayer.start();
      Toast.makeText(getApplicationContext(), &quot;Reproduciendo audio&quot;, Toast.LENGTH_SHORT).show();
  }}
  </code></pre>
  <p>This is the manifest file:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  </code></pre>
  <p></p>
  <pre><code>&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;/&gt;
  &lt;uses-permission android:name=&quot;android.permission.RECORD_AUDIO&quot;/&gt;
  
  &lt;application
      android:allowBackup=&quot;true&quot;
      android:icon=&quot;@mipmap/ic_launcher&quot;
      android:label=&quot;@string/app_name&quot;
      android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
      android:supportsRtl=&quot;true&quot;
      android:theme=&quot;@style/AppTheme&quot;&gt;
      &lt;activity android:name=&quot;.MainActivity&quot;&gt;
          &lt;intent-filter&gt;
              &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  
              &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  &lt;/application&gt;
  </code></pre>
  
  And what logcat says:
  <pre><code>E/MediaRecorder: stop called in an invalid state: 4
  D/AndroidRuntime: Shutting down VM
  E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.recorder, PID: 5386
      java.lang.IllegalStateException: Could not execute method for android:onClick
          at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
          at android.view.View.performClick(View.java:7870)
          at android.widget.TextView.performClick(TextView.java:14970)
          at android.view.View.performClickInternal(View.java:7839)
          at android.view.View.access$3600(View.java:886)
          at android.view.View$PerformClick.run(View.java:29363)
          at android.os.Handler.handleCallback(Handler.java:883)
          at android.os.Handler.dispatchMessage(Handler.java:100)
          at android.os.Looper.loop(Looper.java:237)
          at android.app.ActivityThread.main(ActivityThread.java:7948)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
       Caused by: java.lang.reflect.InvocationTargetException
          at java.lang.reflect.Method.invoke(Native Method)
          at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
          at android.view.View.performClick(View.java:7870) 
          at android.widget.TextView.performClick(TextView.java:14970) 
          at android.view.View.performClickInternal(View.java:7839) 
          at android.view.View.access$3600(View.java:886) 
          at android.view.View$PerformClick.run(View.java:29363) 
          at android.os.Handler.handleCallback(Handler.java:883) 
          at android.os.Handler.dispatchMessage(Handler.java:100) 
          at android.os.Looper.loop(Looper.java:237) 
          at android.app.ActivityThread.main(ActivityThread.java:7948) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075) 
       Caused by: java.lang.IllegalStateException
          at android.media.MediaRecorder._stop(Native Method)
          at android.media.MediaRecorder.stop(MediaRecorder.java:1440)
          at com.example.recorder.MainActivity.Recorder(MainActivity.java:55)
          at java.lang.reflect.Method.invoke(Native Method) 
          at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409) 
          at android.view.View.performClick(View.java:7870) 
          at android.widget.TextView.performClick(TextView.java:14970) 
          at android.view.View.performClickInternal(View.java:7839) 
          at android.view.View.access$3600(View.java:886) 
          at android.view.View$PerformClick.run(View.java:29363) 
          at android.os.Handler.handleCallback(Handler.java:883) 
          at android.os.Handler.dispatchMessage(Handler.java:100) 
          at android.os.Looper.loop(Looper.java:237) 
          at android.app.ActivityThread.main(ActivityThread.java:7948) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075) 
  I/Process: Sending signal. PID: 5386 SIG: 9
  </code></pre>
  <p>Thanks!</p>