android - Kill audio after deliberate or accidental exit of dialog -
this part of code in mainactivity. stopwatch1 image button called in mainactivity:
stopwatch1 = (imagebutton)findviewbyid(r.id.stopwatch1);
i have tried searching clues on how use dialog.setondismisslistener, however, there aren't proper answers me killing of audio after deliberate or accidental exit of dialog. appreciated.
stopwatch1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { final dialog mmdialog1 = new dialog(context); mmdialog1.setcontentview(r.layout.countdowntimer); mmdialog1.settitle("stop-watch"); button ddbutton1 = (button) mmdialog1.findviewbyid(r.id.countdown_exit); final button ddbutton2 = (button) mmdialog1.findviewbyid(r.id.countdown_start); final textview timeview = (textview) mmdialog1.findviewbyid(r.id.timeview); ddbutton2.settag(1); ddbutton2.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { final int status = (integer) v.gettag(); if (status == 1) { ddbutton2.settext("stop"); countdowntimer = new countdowntimer(60000, 1000) { public void ontick(long millisuntilfinished) { int time = (int) (millisuntilfinished/1000); timeview.settext(integer.tostring(time)); } @override public void onfinish() { timeview.settext("60"); ddbutton2.settext("start"); mediaplayer stop = mediaplayer.create(context, r.raw.stop); stop.start(); } }.start(); v.settag(0); } else { ddbutton2.settext("start"); timeview.settext("60"); countdowntimer.cancel(); v.settag(1); } } }); ddbutton1.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { mmdialog1.dismiss(); } }); mmdialog1.show(); } });
my suggestion implement interface. when dialog closes, have callback shut off media player.
a quick example this.
interface
public interface dialogclosed { void shutdownmediaplayer(); }
then implement interface in calling activity
activity
public class mainactivity extends activity implements dialogclosed { .... @override public void shutdownmediaplayer() { //shut down media player here } }
also, may add boolean check if dialog showing this
boolean isshowing = mdialog.isshowing();
and can shut off media player @ point if false.
Comments
Post a Comment