title: How to turn off MediaPlayer on back / home pressed
body: <p>I'm looking for a answer, how can I close MediaPlayer when usep will press "back" or "home" button on device.</p>
  
  <p>I have errors when I press back / home or...
  Everything is ok when press home but error occure when app is restore.
  Can anyone help me with that, I cant find any working code :( ?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>package com.gwynbleiddesign.dzwiekizwierzat;
  
  import android.media.MediaPlayer;
  import android.os.Bundle;
  import android.app.Activity;
  import android.graphics.Typeface;
  import android.view.Menu;
  import android.view.View;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  
  
  public class AnimalDescription extends Activity {
  
      ImageView animalImage;
      TextView txtNazwa, txtOpisZwierzecia;
      int selectedAnimal = 0;
      MediaPlayer mPlayer = null;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_animal_description);
  
          Typeface segoeFont=Typeface.createFromAsset(getAssets(),"fonts/segoeprb.ttf");
  
  
          txtNazwa = (TextView)findViewById(R.id.txtAnimalName);
          txtNazwa.setTypeface(segoeFont);
          txtOpisZwierzecia = (TextView)findViewById(R.id.txtAnimalDescription);
          txtOpisZwierzecia.setTypeface(segoeFont);
          animalImage = (ImageView)findViewById(R.id.imgAnimalPhoto);
  
          selectedAnimal = getIntent().getExtras().getInt("selectedAnimal");
  
          txtNazwa.setText(dane.nazwy[selectedAnimal]+"");
          animalImage.setImageResource(dane.zdjecia[selectedAnimal]);
  
          mPlayer = MediaPlayer.create(this,dane.dzwieki[selectedAnimal]);
  
          animalImage.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
  
          mPlayer.start();    
              }
          });
      }
       @Override 
          public void onBackPressed(){  
                mPlayer.reset();
                mPlayer.release();
                mPlayer = null;
                super.onBackPressed();
          }
  
       @Override 
          public void onPause(){
  
                //mPlayer.reset();
                mPlayer.release();
                mPlayer = null;
                super.onPause();
          }
  
       @Override 
          public void onStop(){
  
  
                mPlayer.reset();
                mPlayer.release();
                mPlayer = null;
                super.onStop(); 
          }
  }
  </code></pre>