title: On Call HangUp, Media Recorder does not stop and save the recorded call
body: <p>I'm New to Android Programming. I am trying to build a Call Recorder. I am able to detect Incoming calls and start the Media Recorder successfully and record voice. But on Call Disconnect, the Stop() method does not trigger. it stops when i close the app. Everything is working except the stop(). when i close the app the recordings are also saved.
  How do i Stop() the recording after the call is disconnected?</p>
  
  <pre><code>import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.media.MediaRecorder;
  import android.os.Environment;
  import android.telephony.TelephonyManager;
  import java.io.IOException;
  import java.util.Calendar;
  
  public class nCallReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
  
  
  
  
  
  MediaRecorder callrecorder = new MediaRecorder();
  
          if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
              String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
              Calendar c = Calendar.getInstance();
              int tm = c.get(Calendar.DATE);
              int tx = c.get(Calendar.SECOND);
              String timex = tm+"-"+tx;
  
  
              callrecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
              callrecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
              callrecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  
              callrecorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + incomingNumber +"-"+ timex+"-"+tx+".mp4");
              try {
                  callrecorder.prepare();
              } catch (IOException e) {
                  e.printStackTrace();
              }
  
              callrecorder.start();
  
              if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)
                      || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                      TelephonyManager.EXTRA_STATE_OFFHOOK)) {
  
                  callrecorder.stop();
                  callrecorder.release();
              }
  
  
          }
      }
  }
  </code></pre>
  
  <p>**</p>
  
  <h1>EDIT 2</h1>
  
  <p>**
  How Do i Stop() the media Recorder?
  I tried instantiating the </p>
  
  <blockquote>
    <p>MediaRecorder r;</p>
  </blockquote>
  
  <p>Globally but, </p>
  
  <blockquote>
    <p>r.stop()</p>
  </blockquote>
  
  <p>Does not work, because r is not instantiated.</p>
  
  <p>If it is possible to save the state of this MediaRecorder Instance, and use it with the stop method later on, How do i do it?</p>
  
  <pre><code>public class nCallRecorder extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
  
          if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
              //start recording
              String CallNo = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
              MediaRecorder r = new MediaRecorder();
              r.setAudioSource(MediaRecorder.AudioSource.MIC);
              r.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
              r.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
              r.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+CallNo+".mp4");
              try { r.prepare(); } catch(IOException e) { e.printStackTrace(); }
              r.start();
  
          } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)) {
              //stop recording
              Toast.makeText(context,"Call Ends",Toast.LENGTH_LONG).show();
          }
      }
  }
  </code></pre>