Skip to content Skip to sidebar Skip to footer

How To Stop Audio From Playing When Back Button Is Pressed?

Hi all here is my code: final ImageView imageView = (ImageView) findViewById(R.id.Image7); imageView.setImageResource(mFullSizeIds[position]); imageView.setOnClickListener

Solution 1:

Add

@OverridepublicvoidonBackPressed()
    {
      if (mp != null)
        mp.stop();
      super.onBackPressed();
    }

and

@Override
public void onPause ()
{
  if (mp != null)
  {
    mp.pause();
    mp.stop();
  }
  super.onPause();
}

Solution 2:

Create an onStop in the activity and make mp a class variable although I always create/prefer media player helper class for this sort of stuff. Then call mp.stop()/pause on onStop.

privateMediaPlayermp=null;

@OverridepublicvoidonStop() {
    super.onStop();

    if (mp != null) {
        mp.stop();
    }
}

then you just create it with removing the MediaPlayer before mp:

mp = MediaPlayer.create(Audio.this,mAudio[position]);

Solution 3:

Override the onBackPressed method, and stop your MediaPlayer object, which is mp.

Post a Comment for "How To Stop Audio From Playing When Back Button Is Pressed?"