Playing Background sound in Android -
i trying play background sound runs throughout application. here have 3 activities , sound started when main activity launches. want achieve these: 1) play bg sound continuously throughout app whatever activity loads.
2) switch off sound when user clicks sound off button. 3) stop sound when app closes.
so far have tried code start sound keeps on playing if app closed.
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); audioplayer(); } boolean isplayingsound = true; public void onclicksound(view view) { final button btn1 = (button) findviewbyid(r.id.button3); if(isplayingsound){ btn1.setbackgroundresource(r.drawable.sound00); isplayingsound=false; audioplayer(false);/*sound doesn't stops here*/ } else{ btn1.setbackgroundresource(r.drawable.sound11); isplayingsound=true; audioplayer(true); } } public void audioplayer(boolean status){ mediaplayer mp = mediaplayer.create(this, r.raw.bg); if(status) { mp.start(); } else { mp.stop(); } }
can plz have , me out here. help!!
you can playback audio move service. can reference asop music code or
as discussed in services document, can create service both started , bound. is, service can started calling startservice(), allows service run indefinitely, , allow client bind service calling bindservice().
if allow service started , bound, when service has been started, system not destroy service when clients unbind. instead, must explicitly stop service, calling stopself() or stopservice().
Comments
Post a Comment