0

On applying Picture in Picture mode to a YouTube Player View and while running the app, when I enter the Picture in Picture mode, the Video playing in YouTube Player pauses and the play button cant be clicked due to PiP's own layout. How can I keep playing the video as I enter in the PiP mode from the same current time?

Here is my Code:-

`public class Video extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

    YouTubePlayerView playerView;
    YouTubePlayer.OnInitializedListener onInitializedListener;
    Button pip_button;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_video);
        pip_button = findViewById(R.id.pip_button);
        pip_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (android.os.Build.VERSION.SDK_INT >= 26) {
                    //Trigger PiP mode
                    try {
                        Rational rational = new Rational(playerView.getWidth(), playerView.getHeight());

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

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

        });
        playerView = findViewById(R.id.playerview);
        playerView.initialize("api_key", this);
}
    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
        youTubePlayer.cueVideo(videoID);
        youTubePlayer.play();
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
        Toast.makeText(this, "Something Went Wrong! Please Try Again!", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
        onInitializedListener = new YouTubePlayer.OnInitializedListener() {

            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {

                youTubePlayer.cueVideo(videoID);
                youTubePlayer.pause();
                youTubePlayer.play();

            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

            }
        };
    }
  • please check this answer: https://stackoverflow.com/a/54061449/5339146 – Mihodi Lushan Aug 06 '20 at 19:58
  • I've gone through it and other posts also but the Youtube Player keeps pausing even if I resume/play it. – Fizanto Fuzz Aug 07 '20 at 06:59
  • can you please check your LogCat, I faced the same issue once because unconsciously I added a TextView (which was overlapping the Youtube Player View) to show a Snackbar when the network state changes, even if that textview was visibility gone, the player was pausing again and again. I suggest you recheck your XML again and make sure no other views are overlapping. thanks – Mihodi Lushan Aug 08 '20 at 06:40
  • Yeah! Nothing's wrong with my XML code... My video player runs smoothly in the normal mode. But as soon as I enter the Picture in Picture mod it loops through pauses and plays... – Fizanto Fuzz Aug 09 '20 at 07:04
  • As I told you earlier, Please check your LogCat and add your warnings/errors shown in LogCat. To check LogCat, press the Logcat button (bottom bar in Android Studio) then run your application, make sure you are checking the correct device (check in the dropdown and make sure the device is not dead) and Process, also select show only selected application from the rightmost dropdown. then whenever your video is getting paused, copy the LogCat texts and add to your question for reference. – Mihodi Lushan Aug 09 '20 at 10:58

0 Answers0