androidandroid-youtube-apiandroid-picture-in-picture

How to Add Picture-In-Picture mode onTouch Drag Down


I am using this Github project for my app to play you tube videos using Youtube API.. I have the following code in my onInitializationSuccess method..

 @Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
    this.player = player;
    player.setPlayerStateChangeListener(playerStateChangeListener);
    player.setPlaybackEventListener(playbackEventListener);

    if (!wasRestored) {
        player.cueVideo("I0D-fkypQQw");
    }
}

I coded this YouTubePlayerView to play in PIP mode in the following way :

enter_pip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //  Intent i = new Intent(getBaseContext(), PipActivity.class);
            //  startActivity(i);
            if (android.os.Build.VERSION.SDK_INT >= 26) {
                //Trigger PiP mode
                try {
                    Rational rational = new Rational(youTubeView.getWidth(), youTubeView.getHeight());

                    PictureInPictureParams mParams =
                            new PictureInPictureParams.Builder()
                                    .setAspectRatio(rational)
                                    .build();

                    enterPictureInPictureMode(mParams);
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(MainActivity.this, "API 26 needed to perform PiP", Toast.LENGTH_SHORT).show();
            }
        }

    });
}

The video successfully enters the PIP Mode, but the problem is the video stops after entering PIP mode , i want it to continue from where it was when not in PIP mode..

Please guide me on adding the picture-in-picture mode wherein the picture in picture mode should continue playing rather than stop.. I am looking for a youtube like PIP wherein the video plays at the bottom with Play/Pause Button and Close Button on its right..
I am looking for a PIP as in this image


Solution

  • Browse this GitHub Link which completely meets your criteria of a DragDown Video...

    To summarize the github project... use the goMin() method code ..in the YouTuDraggingView class which is as below:

        public void goMin(final boolean isDismissToMin) {
        nowStateScale = MIN_RATIO_HEIGHT;
        final float fullTop = Math.abs(mBackgroundViewWrapper.getMarginTop() - mRangeScrollY);
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(mBackgroundViewWrapper.getMarginTop(), mRangeScrollY);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mBackgroundView.setAlpha(isDismissToMin ? (value / fullTop) : 1);
                updateVideoView((int) value);
                if (value == mRangeScrollY) {
                    ViewGroup.LayoutParams p = getLayoutParams();
                    p.width = -2;
                    p.height = -2;
                    setLayoutParams(p);
                    mCallback.status(STATUS_MIN);
                }
            }
        });
        valueAnimator.setDuration((long) (Math.abs((1 - mBackgroundViewWrapper.getMarginTop() / mRangeScrollY)) * rangeDuration)).start();
      }
    

    and the goMax() to reverse...

        public void goMax() {
        if (nowStateScale == MIN_RATIO_HEIGHT) {
            ViewGroup.LayoutParams params = getLayoutParams();
            params.width = -1;
            params.height = -1;
            setLayoutParams(params);
        }
    
    
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(mBackgroundViewWrapper.getMarginTop(), 0);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
    
                updateVideoView((int) value);
                if (value == 0) {
                    if (isLandscape()) {
                        goFullScreen();
                    }
                    mCallback.status(STATUS_MAX);
                }
            }
        });
        valueAnimator.setDuration((long) (mBackgroundViewWrapper.getMarginTop() / mRangeScrollY * rangeDuration)).start();
    
        nowStateScale = 1.0f;
      }
    

    Hope this will help you...