处理Android Pie上的后退按钮VideoView

时间:2018-12-18 09:51:31

标签: android android-videoview onbackpressed android-9.0-pie

我将VideoViewMediaController一起使用。我正在解决常见的back-press bug,因此我们可以找到很多与此相关的主题:

Android back button and MediaController

Back button won't work when VideoView is playing video

First Back button press not caught when playing a video android

Problem with back button in VideoView

所有这些都建议覆盖dispatchKeyEvent中的MediaController。但这不会在Android Pie上触发...该方法在较旧的OS版本上适用,但是在Pie上,当媒体控件可见时,我没有收到dispatchKeyEventonKeyPressed的调用-{{1 }}(也选中了Activity),包括onBackPressed(在整个运行期间都处于焦点状态)和View的任何VideoView。事实上,只要MediaController在屏幕上可见,后退按钮就不会起作用(既不关闭MediaController也不隐藏Activity),而且我不知道“谁”消耗了那件事...

MediaController

当我删除上面的代码时,一切正常,但是我需要这些媒体控件

一年前有人遇到类似的问题,没有任何答案...(请注意,Pie年龄较小……)

Android back button not working while playing video in VideoView

2 个答案:

答案 0 :(得分:0)

在我自己找到解决方案之前,我还在寻找解决方案:

在MediaController中,您必须添加一个OnUnhandledKeyEventListener。

 public bool OnUnhandledKeyEvent(View v, KeyEvent e)
    {
        if (e.KeyCode == Keycode.Back && e.Action == KeyEventActions.Up)
        {
            ...
        }
        return true;
    }

对我而言(使用Xamarin)看起来像:

public class ExtMediaController : MediaController
{
    public delegate void CallBackButtonDelegate();
    public CallBackButtonDelegate BackEvent;
    public ExtMediaController(Context context) : base(context)
    {
        base.AddOnUnhandledKeyEventListener(new OnUnhandledKeyEventListener(this));
    }


    public override bool DispatchKeyEvent(KeyEvent e)
    {
        if (e.KeyCode == Keycode.Back)
        {
            BackEvent?.Invoke();
            //return base.DispatchKeyEvent(e);
        }  

        return base.DispatchKeyEvent(e);
    }

}


 public class OnUnhandledKeyEventListener : Java.Lang.Object, IOnUnhandledKeyEventListener
{
    private ExtMediaController LinkedMediaController;

    public OnUnhandledKeyEventListener(ExtMediaController extMediaController)
    {
        LinkedMediaController = extMediaController;
    }

    public bool OnUnhandledKeyEvent(View v, KeyEvent e)
    {
        if (e.KeyCode == Keycode.Back && e.Action == KeyEventActions.Up)
        {
            LinkedMediaController.BackEvent?.Invoke();
        }
        return true;
    }
}

答案 1 :(得分:0)

我扩展了mediacontroller类,以便它可以对所有版本的android(包括android P)正确处理后台活动。您可以将该类实例化为媒体控制器。

public class ExtendedMediaController extends MediaController {
private Activity            mParentActivity;

public ExtendedMediaController(Context context, Activity parentActivity) {
    super(context);
    mParentActivity = parentActivity;

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        OnUnhandledKeyEventListener eventListener = new OnUnhandledKeyEventListener() {
            @Override
            public boolean onUnhandledKeyEvent(View v, KeyEvent event) {
                boolean fHandled = false;

                if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                    if (event.getAction() == KeyEvent.ACTION_DOWN) {
                        fHandled =  true;
                    } else if (event.getAction() == KeyEvent.ACTION_UP) {
                        if(mParentActivity != null) {
                            mParentActivity.onBackPressed();
                            fHandled = true;
                        }
                    }
                }
                return(fHandled);
            }
        };
        addOnUnhandledKeyEventListener(eventListener);
    }

}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    boolean fHandled = false;
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            fHandled =  true;
        } else if (event.getAction() == KeyEvent.ACTION_UP) {
            mParentActivity.onBackPressed();
            fHandled = true;
        }
    }
    if(!fHandled) {
        fHandled = super.dispatchKeyEvent(event);
    }
    return(fHandled);
}


}