ActionBar overlaying YouTubePlayerSupportFragment from YouTube Player API

若如初见. 提交于 2019-12-04 15:26:12

Ok, so as there is really no support for the Android YouTube Player API, I'll answer with how you actually have to do it for it to work.

First, drop the idea of using the ActionBar. It's an old API, it's ugly to have the controls in it, it's a pain to handle and it doesn't even work if you're not using the sample given by Google. The rest of this answer will explain a working method on how to add controls on top of the YouTube Player, but if you really want to use the ActionBar, you will not find an answer here (nor anywhere else as far as I know).

Fine. Now, for this to work you can use the YouTube Player normally. I recommend using the Fragment, as having to extend an existing Activity class can be a problem. Remove all controls of the Player using player.setPlayerStyle(PlayerStyle.CHROMELESS) in the OnInitializationSuccess callback.

Now, to get your controls on top of the Player without it pausing, you need to use a DialogFragment (Using a Dialog alone may work, but again here using the Fragment brings more controls on what you can do):

  • Create your own subclass of DialogFragment
  • Override onCreate(Bundle) and call setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Translucent_NoTitleBar)
  • Override onCreateView(LayoutInflater, ViewGroup, Bundle) and recreate your Activity layout here, however you want. I'm using Kotlin and Anko, so everything is dynamic for me, I have no XML. Anyway, recreate your Layout but without the final views (the result should be entirely invisible).
  • Now, in the view which replaces your player, let's say you used a FrameLayout, you can add your controls like if it was the Player's FrameLayout
  • Override onStart() and call the following methods on the now created Dialog, to make sure your Dialog is fullscreen and transparent:

    Dialog dialog = getDialog();
    dialog.setCanceledOnTouchOutside(false);
    dialog.setCancelable(false);
    dialog.setOnKeyListener(new OnKeyListener() {
    
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                // TODO Dismiss the Dialog AND call onBackPressed() on the underlying Activity
                return true;
            } else {
                return false;
            }
        }
    
    });
    
    Window window = getWindow();
    window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    WindowManager.LayoutParams layoutParams = window.getAttributes();
    layoutParams.setDimAmount(0f);
    layoutParams.setFlags(layoutParams.getFlags() | WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    window.setAttributes(layoutParams);
    

Now you'll have to handle your controls, make them disappear after a timer, etc. Just make sure that you put everything that will cover the Player at some point inside the Dialog(Fragment).

I'm not sure that everything I listed here is mandatory for this to work, but that's how I did it and it works.

Note: As I said, I'm working with Kotlin and Anko, I didn't write Java for some months now, so any code presented here may have small typos. Please just tell me if you see any error.

Bonus: How I handled Fullscreen.

To handle fullscreen mode, I simply set the visibility of everything but the Player AND the matching view (a FrameLayout for me) in the Dialog to GONE, the make sure that LayoutParams' width and height of those two views are set to MATCH_PARENT. To exit fullscreen, just set all views' visibility you changed back to VISIBLE.

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