java - Android backround music across multiple activities; How to catch Home button presses -


i have been searching days on issue , while has been brought on , on again here on , other places haven't found single proper solution.

i want play audio file in background of app. easy enough. want music persist , not stop or pause while switching between activities in app. easy , accomplished doing in oncreate method:

    protected void oncreate(bundle savedinstancestate) {       super.oncreate(savedinstancestate);       ...       mp = mediaplayer.create(mainactivity.this, r.raw.lostmexicancity);       mp.setlooping(true);       mp.start();     } 

the problem? getting music stop when press home button.

killing sound when user presses button seems easy. here's have , works great:

    public void onpause() {        if(this.isfinishing()){  //back pressed activity         mp.stop();       }      super.onpause(); } 

pretty simple right? yeah. not complicated. not catch presses of home button. if home button pressed, music keeps playing while user no longer sees app. in hours , hours of searching have not been able find simple solution this.

i have seen answers involve setting permission in manifest tasks shouldn't have , appears dangerous users. besides solution didn't work. i've seen solutions involve using service, none of work either because home button still plays music before because there doesnt seem way catch , doesn't 'finish' app (not mention every time suggest using service task multiple people come in , state not proper use services)

it seems way kill music when home button pressed use non-conditional stop() within onpause, that's no because that's called when swap activities intents, causing music end between activities no good.

i have trouble imagining such exceedingly common function background music hard ive seen post after post same issue me , no proper answers other ones kill music between activities within app.

how on earth other apps on google play store accomplish , yet there appears no clear answer online? mean, stop , start music each onpause() cause unprofessional gaps in audio not mention start background audio beginning on , on again unacceptable.

there has overlooking here. thank in advance help.

i'm bit new android programming (few months) , today, faced same problem did (maybe still do?)

i made work following :

lets have mainactivity, , in mainactivity have btn2 leads secondactivity, , btn3 leads thirdactivity.

i declared @ beginning of mainactivity :

public static boolean shouldplay = false; 

i implemented onstop() method :

public void onstop() {     super.onstop();     if (!shouldplay) { // won't pause music if shouldplay true         player.pause();         player = null;     } } 

if boolean shouldplay set true, onstop() won't called entirely , music won't turn off. have decide when set true. when switch mainactivity secondactivity, through intent , that's when i'll set shouldplay true :

button btn2 = (button) findviewbyid(r.id.btn2);     btn2.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view v) {             intent intent = new intent(mainactivity.this, secondactivity.class);             shouldplay = true;             startactivity(intent);         }     }); 

and same done btn3.

now, last thing want looking if go mainactivity after visiting secondactivity or thirdactivity, shouldplay have been set true. first thing tried set false second , thirdactivity called (in oncreate()) want work, maybe because onstop() main , oncreate() others called simultaneously (frankly don't life cycle now). worked set shouldplay false every time launch oncreate() of main :

shouldplay = false; 

this works me. let me know if you, cheers, bro.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -