Search code examples
androidandroid-mediaplayermediacontroller

Android back button on media controller for videos played from webView


I am displaying a video in a webView and I am noticing particular behaviors from Kindle devices. First, you have to slide out the device buttons (on Kindle Fire HD), which brings up the video controls for the video. Pressing the back button while the controls are up will first hide the video controls. And then you have to press the back button again to dismiss the view. I have been trying to dismiss the view on first back press even while controls are up. The conflict I am running into is that I cannot capture that first back button press while the controls are up. Once the controls are gone and I press the back button it shows up in my @Override onBackPressed() OR the below dispatchKeyEvent() I added to try and resolve this issue.

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
    switch (keyCode) {
    case KeyEvent.KEYCODE_BACK:
        if (action == KeyEvent.ACTION_DOWN) {
            // TODO
            finish();
        }
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}

Pressing the back button twice (after controls are gone) will register the keyEvent. But you still have to press it twice. Has anyone else ran into this conflict and/or do you know of a fix? I added both the dispatchkeyEvent() and onBackPressed() because I was hoping the dispatchKeyEvent() would catch the first back press, and then onBackPressed() would catch the second OR dispatchKeyEvent() would catch both. I get nothing while the controls are up for the first back press. Thank you in advance!


Solution

  • ACTION_DOWN will provide you with the next available child action in the hierarchy. However, I think you meant ACTION_UP, which takes you to the parent screen. You should also try putting finish() before the if statement.