android mediaplayer - How to stop music when user exits the app -


i reading many tips , answers similar questions can't done piece of code. isse music played in background when exit app. can me fix this? have button clicked starts music , clicked second time pause it. works want achieve stop music play, when user exits app eather clicking home or 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();          }       });     } 

try overriding onpause method

@override public void onpause() {     super.onpause();     //pause player     mp.pause();     //do more stuff } 

when opening again if want start playing again override onresume:

@override public void onresume() {     super.onresume();     //play again     mp.play();     //do more stuff } 

hope helps


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -