title: Android MediaPlayer : IllegalStateException when app closes/press back button
body: <p>Im streaming an mp3 audio from a url by using mediaplayer, Now im able to play music , But when <strong>i press back button or close the app, it crashes.</strong>
  can anyone pls help me to find my mistake. 
  thank you.</p>
  
  <p><em>My code is :</em></p>
  
  <pre><code>        private ImageView play, forward, backward;
          private MediaPlayer mediaPlayer;
          private boolean playing = false;
          private ProgressDialog dialog;
          private String mp3link;
          private SeekBar seekbar;
          private Handler handler = new Handler();
          private int mediaPos;
          private int mediaMax;
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              final String url ="";
              initWidgets();
  
          }
  
      private void initWidgets() {
          mp3link = "http://loc8app.com/church/uploads/audio/749928ad6fcb7b1aceefdf03bd7a9465.mp3";
          play = (ImageView) findViewById(R.id.control);
          seekbar = (SeekBar) findViewById(R.id.seekBar);
  //        forward = (ImageView) findViewById(R.id.playeer_forward);
  //        backward = (ImageView) findViewById(R.id.playeer_back);
          mediaPlayer = new MediaPlayer();
          play.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  playFunction();
              }
          });
  
          seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  
              @Override
              public void onStopTrackingTouch(SeekBar seekBar) {
  
              }
  
              @Override
              public void onStartTrackingTouch(SeekBar seekBar) {
  
              }
  
              @Override
              public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                  if(mediaPlayer != null &amp;&amp; fromUser){
                      mediaPlayer.seekTo(progress);
                  }
              }
          });
      }
  
  
      private void playFunction() {
          if (!playing) {
              try {
                  dialog = ProgressDialog
                          .show(MainActivity.this,
                                  "",
                                  getString(com.root5solutions.music.R.string.buffering),
                                  true);
                  dialog.setCancelable(true);
                  dialog.show();
                  mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                  mediaPlayer.setDataSource(mp3link);
                  mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
  
                      @Override
                      public void onPrepared(MediaPlayer mp) {
                          play.setBackgroundResource(R.drawable.pause);
                          playing = true;
                          //this is new
                          mediaPos = mp.getCurrentPosition();
                          mediaMax = mp.getDuration();
  
                          seekbar.setMax(mediaMax);
                          seekbar.setProgress(mediaPos);
                          //this line is the error
                          handler.removeCallbacks(moveSeekBarThread);
                          handler.postDelayed(moveSeekBarThread, 100);
  
                          mp.start();
                          dialog.dismiss();
                      }
                  });
                  mediaPlayer.prepareAsync();
  
              } catch (Exception e) {
                  e.printStackTrace();
                  dialog.dismiss();
              }
          } else {
              play.setBackgroundResource(R.drawable.play);
              mediaPlayer.stop();
              mediaPlayer.release();
              playing = false;
          }
      }
  
      @Override
      public void onBackPressed() {
          super.onBackPressed();
          if (mediaPlayer.isPlaying()) {
              mediaPlayer.stop();
              mediaPlayer.release();
          }
      }
  
      private Runnable moveSeekBarThread = new Runnable() {
  
          public void run() {
              if (mediaPlayer.isPlaying()) {
                  int mediaPos_new = mediaPlayer.getCurrentPosition();
                  int mediaMax_new = mediaPlayer.getDuration();
                  seekbar.setMax(mediaMax_new);
                  seekbar.setProgress(mediaPos_new);
  
                  handler.postDelayed(this, 1000); // Looping the thread after 1 second
  
              }
  
          }
      };
  }
  </code></pre>
  
  <p><em>Logcat shows :</em></p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.root.music, PID: 26981
                                                                            java.lang.IllegalStateException
                                                                                at android.media.MediaPlayer.isPlaying(Native Method)
                                                                                at com.root.music.MainActivity$4.run(MainActivity.java:132)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:135)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5351)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:947)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
  </code></pre>