android - overriding back button causes volume buttons stop working
Asked Answered
S

4

1

I'm trying to override Back button to stop an audio player and close media player activity, but I noticed that volume buttons are not working anymore. I imagine there is a mistake in overriding code. PS. in my MediaController I override hide() method to always show controls.

mediaController = new MediaController(this)

{
    @Override
    public void hide() {
        // TODO Auto-generated method stub
        //do nothing
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {

            if (mediaPlayer != null) {
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer = null;

            }
            super.hide();

            Activity a = (Activity)getContext();
            a.finish();

        }
        return true;
    }
};
Seignior answered 28/4, 2014 at 10:44 Comment(2)
I have had the same issue, see my answer :)Microvolt
i have updated the ans try thatMaricruzmaridel
M
5

Your problem is in dispatchKeyEvent():

You are using return true for all the HW Keys and this stops all keys from responding,

whereas you should be using

return super.dispatchKeyEvent(event);

For keys other than Back Key.

So your code should be something like this:

@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
    if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {

        if (mediaPlayer != null) {
            mediaPlayer.reset();
            mediaPlayer.release();
            mediaPlayer = null;
        } 

        finish();
        return true;
    }
    **return super.dispatchKeyEvent(event);**
}
Microvolt answered 28/4, 2014 at 11:36 Comment(1)
I understood what you mean and your code let me use volume buttons as aspected, but when I launch the activity and press back button it crashes with this message: Attempt to invoke virtual method 'int android.media.MediaPlayer.getCurrentPosition()' on a null object referenceSeignior
F
2

You could overide onBackPressed() instead.

Forster answered 28/4, 2014 at 10:47 Comment(0)
B
2

You should use like this

@Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK) {

            if (mediaPlayer != null) {
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer = null;
            } 

            finish();
            return true;
        }
        return false;
    }

Out the code from method or try :

@Override
public void onBackPressed() {
     if (mediaPlayer != null) {
          mediaPlayer.reset();
          mediaPlayer.release();
          mediaPlayer = null;
     } 
   finish();
}

or

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
         if (mediaPlayer != null) {
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer = null;
            } 

           finish();
           return true;
    }

    return super.onKeyDown(keyCode, event);
}
Burtis answered 28/4, 2014 at 10:49 Comment(1)
doesn't work, I edited the code, please have a look.Seignior
M
0

Did you tried this check it once it may solve your issue

@Override
    public void onBackPressed() {
         if (mediaPlayer != null) {
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer = null;
            } 

           finish();


    }
Maricruzmaridel answered 28/4, 2014 at 10:54 Comment(4)
where did you get eventWilbourn
why you need to cast context to Activity. onBackPressed is a method of Activity. So what is missing is a call to super.onBackPressed()Wilbourn
ok that is also right @blackbelt he want to finish the activityMaricruzmaridel
doesn't work, I edited the code, please have a look.Seignior

© 2022 - 2024 — McMap. All rights reserved.