I was reading many tips and answers to similar questions but can't get it done with my piece of code. The isse is that music is played in background even when you exit the app. Can you help me to fix this? I have the button which clicked starts the music and clicked the second time pause it. This works what I want to achieve is to stop music play, when the user exits the app eather by clicking home or back button.
ImageButton playmusic;
playmusic = (ImageButton)this.findViewById(R.id.listen_button);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.music);
playmusic.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(mp.isPlaying() == true)
mp.pause();
else
mp.start();
}
});
}