title: Android studio E/MediaRecorder﹕ stop called in an invalid state: 4
body: <p>I am trying to create a simple record and play audio application for android using MediaRecorder and MediaPlayer when i try to stop  recording i get in the logcat the following error E/MediaRecorder﹕ stop called in an invalid state: 4 i am testing on a moto g 2013 16 gb memory version 5.1 </p>
  
  <p>Main activity</p>
  
  <pre><code>package com.example.sebastin.myapplication;
  
  import android.content.DialogInterface;
  import android.graphics.Color;
  import android.hardware.Sensor;
  import android.hardware.SensorEvent;
  import android.hardware.SensorEventListener;
  import android.hardware.SensorManager;
  import android.location.GpsStatus;
  import android.media.MediaPlayer;
  import android.media.MediaRecorder;
  import android.os.Environment;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.FilterWriter;
  import java.io.IOException;
  
  
  public class MainBina extends AppCompatActivity {
      TextView texto;
      Button boton ,boton2, boton3, boton4;
      MediaPlayer Play;
      MediaRecorder Record;
      String grabacion;
      String filePath;
      String grabarState;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main_bina);
          filePath  = Environment.getExternalStorageDirectory()+ "/audiorecordtest.3gp";
          texto = (TextView) findViewById(R.id.textView);
          //boton.setText("Grabar");
          boton = (Button) findViewById(R.id.button);
          boton2 = (Button) findViewById(R.id.button2);
          boton3 = (Button) findViewById(R.id.button3);
          texto.setText(boton.getText().toString());
          boton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  switch ( boton.getText().toString())
                  {
                      case "grabar":
                          try {
                              startRecord();
                          } catch (Exception e) {
                              e.printStackTrace();
                          }
                          boton.setText("grabando");
                          break;
                      case "grabando":
                          try {
                              stopRecord();
                          } catch (Exception e) {
                              e.printStackTrace();
                          }
                          boton.setText("Grabar");
                          break;
                  }
  
  
              }
          });
          boton3.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  stopPlay();
              }
          });
      boton2.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
  
              try {
                  startPlay();
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      });}
      public void startRecord ()throws Exception{
          if (Record!=null)
          {
              Record.release();
          }
          File fileOut = new File(filePath);
          if (fileOut!=null)
          {
              fileOut.delete();
          }
          String fileName = "bina1.3gpp";
          FileOutputStream fileOutputStream = openFileOutput(fileName, MODE_PRIVATE);
          String filePathh = fileOutputStream.toString();
  
          texto.setText(filePath);
          Record = new MediaRecorder();
          Record.setAudioSource(MediaRecorder.AudioSource.MIC);
          Record.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
          Record.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
          Record.setOutputFile(filePath);
          fileOutputStream.close();
          Record.prepare();
  
          Record.start();
  
      }
      public void stopRecord () {
          Record.stop();
          Record.reset();
          Record.release();
          Record = null;
      }
      public void startPlay  ()throws Exception {
          Play = new MediaPlayer();
          Play.setDataSource(filePath);
          Play.prepare();
          Play.start();
          Play.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
              @Override
              public void onCompletion(MediaPlayer Play) {
                  Play.release();
              }
          });
      }
      public void stopPlay ()
      {
          if (Play != null) {
              Play.stop();
              Play.release();
              Play = null;
          }
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main_bina, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
  }
  </code></pre>
  
  <p>XML</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"
      tools:context=".MainBina"
      android:id="@+id/linear"
      android:orientation="vertical"&gt;
  
  
      &lt;TextView android:text="@string/saludo"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="center_vertical"
          android:id="@+id/textView"
          android:layout_gravity="center_horizontal" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/Grabar"
          android:id="@+id/button"
          android:layout_marginTop="35dp" /&gt;
  
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/Reproducir"
          android:id="@+id/button2" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/Parar"
          android:id="@+id/button3" /&gt;
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Manifest </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.sebastin.myapplication" &gt;
      &lt;uses-permission android:name="android.permission.RECORD_AUDIO" /&gt;
      &lt;uses-permission android:name="android.permission.write_external_storage" /&gt;
      &lt;application
  
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name=".MainBina"
              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;/application&gt;
  &lt;/manifest&gt;
  </code></pre>