Viewpager with Youtube Player API

510 Views Asked by At

I have used ViewPager. And In some of the items, I have used Youtube Player Fragment.

I used PageTranformers from this Android Dev Page

public class DepthPageTransformer implements ViewPager.PageTransformer {
    private static final float MIN_SCALE = 0.75f;

    public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();

        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.setAlpha(0);

        } else if (position <= 0) { // [-1,0]
            // Use the default slide transition when moving to the left page
            view.setAlpha(1);
            view.setTranslationX(0);
            view.setScaleX(1);
            view.setScaleY(1);

        } else if (position <= 1) { // (0,1]
            // Fade the page out.
            view.setAlpha(1 - position);

            // Counteract the default slide transition
            view.setTranslationX(pageWidth * -position);

            // Scale the page down (between MIN_SCALE and 1)
            float scaleFactor = MIN_SCALE
                    + (1 - MIN_SCALE) * (1 - Math.abs(position));
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

        } else { // (1,+Infinity]
            // This page is way off-screen to the right.
            view.setAlpha(0);
        }
    }
}

The problem is,

If I used DepthPageTranformer,

when I move to that page(which has youtube fragment) , I am getting warning

W/YouTubeAndroidPlayerAPI: YouTube video playback stopped due to unauthorized overlay on top of player. The YouTubePlayerView is obscured by android.widget.RelativeLayout{28896be5 V.E..... ........ 39420,0-39960,850}. YouTubePlayerView is completely covered, with the distance in px between each edge of the obscuring view and the YouTubePlayerView being: left: 0, top: 0, right: 0, bottom: 510..

But it works if I didn't use any transformer or with ZoomPageTranformer.

I debugged the code with custom ViewPager to check which view is

android.widget.RelativeLayout{28896be5 V.E..... ........ 39420,0-39960,850}

It is the previous page of the page which has Youtube Player Fragment .

I tried all the solutions like,

  1. removing padding and margins
  2. setting background as black
  3. setting all the view's visibility which overlaps as View.GONE

Everything works fine if I don't use DepthPageTranformer.

EDIT

As we know, Viewpager's OFFSCREEN_PAGES=1 by default. So the next and previous page will be in cache. Consider, If player view is in 5th page, If I move to 7th page and move back to 5th page , then video is playing. It clearly says that the "previous page of video page overlays" . Is there any way to refresh this page or remove previous page from OFFSCREEN_PAGES LIST.

Can anyone help me?

1

There are 1 best solutions below

0
On

Just implement this interface YouTubePlayer.OnFullscreenListener Activity or Fragment. It overrides below method,

override fun onFullscreen(isFullScreen: Boolean) {
    if (isFullScreen)
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
    else
        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
}

Why is this happening?

When playing youtube videos using YouTubePlayerFragment or YouTubePlayerView, we need to hide the Status Bar because of no more views will not be added above YouTubePlayerFragment or YouTubePlayerView.

I had also this issue and found a solution.

Just try this one.