YoutubePlayer - Navigation Bar Overlay

好久不见. 提交于 2019-12-01 12:09:35

So this is how went around the problem (this is still not a solution but its the closest I can get to).

So, I added an OnSystemUiChangeListener in the onInitializationSuccess of YoutubePlayerSupportFragment (or YoutubePlayerFragment).

(View) getView().getParent().setOnSystemUiVisibilityChangeListener();

provide an implemented object of OnSystemUiChangeListener.

Override the method onSystemUiVisibilityChange() like so:

@Override
public void onSystemUiVisibilityChange(int visibility) {

    if (visibility == View.SYSTEM_UI_FLAG_VISIBLE) {
        scheduleNavigationBarHide();
    }
    else if (visibility == View.SYSTEM_UI_FLAG_HIDE_NAVIGATION || visibility == View.SYSTEM_UI_FLAG_LOW_PROFILE
            || visibility == View.SYSTEM_UI_FLAG_FULLSCREEN) {
        if (navigationBarHandler != null) {
            navigationBarHandler.cancel();
            navigationBarHandler.purge();
            navigationBarHandler = null;
        }
    }
}

Provide definition for the method scheduleNavigationBarHide().

private void scheduleNavigationBarHide() {


    if (navigationBarHandler != null) {
        Log.d(TAG, "Canceling navigationBarHandler.");
        navigationBarHandler.cancel();
        navigationBarHandler.purge();
        navigationBarHandler = null;
    }

    if (mContext != null && mContext instanceof JadooTVActivity) {
        navigationBarHandler = new Timer();

        navigationBarHandler.schedule(new TimerTask() {
            public void run() {
                ((JadooTVActivity) mContext).runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (!isPlayerSqueezed) {
                            hideAsImmersiveNavigationBar(Config.context);
                        }
                        else {
                            Utils.showNavigationBar(Config.context);
                        }
                    }
                });
            }
        }, 500);
    }
}

So finally hideAsImmersiveNavigationBar() as you might have guessed this works by making the Navigation Bar temporarily immersive. Here's how

private void hideAsImmersiveNavigationBar(Activity activity) {

    if(activity != null)
    {   View decorView = activity.getWindow().getDecorView();

        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE;
        decorView.setSystemUiVisibility(uiOptions);
    }
}

Later when player has closed you might wanna bring back the Navigation Bar. Change the UI Flags to int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE | View.SYSTEM_UI_FLAG_FULLSCREEN;

Finally a little disclaimer: I have provided a list of devices the application has been tested, I don't know if this will work on every devices. If you do find a device where the issue is persistent then feel free to comment. Also Immersive mode was added from API 19 so the solution will not work on any device before that, but I only had the issue on one device running API 21. All the older devices worked well. Also If/when the Navigation Bar stays for 1 second, we still get the overlay error.

change activity to full screen when youtube is fullscreen.

 void toggleFullScreen(boolean goFullScreen){   
    if(goFullScreen){
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    }else{ 
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    } 

    yourView.requestLayout(); 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!