-1

i am searched many solution but could not find proper result when i press the back button of mobile i am using one counter variable and put the condition like when counter is 0 it called the music file. but when i am comeout from that activity means press back button of mobile still that activity is runnig and called the music file .i am using this below code for music play.please anyone have solution then help me.i am using one timer function.like this when i==0 that time call one music file but when i press back button. this timer i wants to stop.

public void btntimer(){

    new Handler().postDelayed((new Runnable() {

        @Override
        public void run() {
            if(i!=32) {
                matchNo();
            }
            else {
                try{
                showdialog();
                media=1;
                }catch (Exception e){
                    media=0;
                }
                updatepoints();
            }
        }
    }), 4000);


}






song=MediaPlayer.create(this,R.raw.cartoon1);
            song.start();
            song.setLooping(false);
ankita
  • 33
  • 4

1 Answers1

0

If I understand you correct, you want to stop playing music on back button pressed.

Call stop() in onBackPressed method.

@Override
public void onBackPressed() {
    song.stop();
    song.release();
    song = null;
}
Virtap
  • 40
  • 6
  • thanks for yr valuable support i am also back in application from second activity to first activity.when i press the back button of mobile i am using yr code but application is unfortunatle stoped. – ankita Aug 28 '15 at 09:14
  • You are saying that on back button pressed, you also want to go from current activity back to first activity. If yes, than simply call finish(); at the end of onBackPressed method. You should also check song for null - in that case you can't call stop() – Virtap Aug 28 '15 at 09:53
  • hi ,i am edit my code you read it..actully in my application .in that i am using one timer function which is runnig and when that value is ==0 that'time call one music file.when i am start to play game in btw i am press the back button the process is runnig till the 0.how can i stop that timer when i press the back button. – ankita Aug 28 '15 at 10:24
  • Ok, i got it. You should look at this answer how to do it: http://stackoverflow.com/questions/4378533/cancelling-a-handler-postdelayed-process (you will call myHandler.removeCallbacks(myRunnable); in onBackPressed method) – Virtap Aug 28 '15 at 11:07
  • hi,i am trying this all yr solution still my problem is not solve.if you have onther solution to stop music file when i press back button of mobile. – ankita Aug 31 '15 at 05:17
  • hey finally yr last solution is working i am using this handler.removecallbackmessege(null); it is workd for me..thanks a lot – ankita Sep 02 '15 at 07:38