3

I have an app which plays video from youtube and i have coded the link in my MainActivity class..

I have made the video played in my app as expected..

I have tried PictureInPictureParams.Builder but ran into multiple errors..

public class MainActivity extends YouTubeBaseActivity implements 
YouTubePlayer.OnInitializedListener {

private static final int RECOVERY_REQUEST = 1;
private YouTubePlayerView youTubeView;
private MyPlayerStateChangeListener playerStateChangeListener;
private MyPlaybackEventListener playbackEventListener;
private YouTubePlayer player;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
    youTubeView.initialize(Config.YOUTUBE_API_KEY, this);

    playerStateChangeListener = new MyPlayerStateChangeListener();
    playbackEventListener = new MyPlaybackEventListener();

    final EditText seekToText = (EditText) findViewById(R.id.seek_to_text);
    Button seekToButton = (Button) findViewById(R.id.seek_to_button);
    seekToButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int skipToSecs = Integer.valueOf(seekToText.getText().toString());
            player.seekToMillis(skipToSecs * 1000);
        }
    });
}

@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"); // Plays https://www.youtube.com/watch?v=fhWaJi1Hsfo
    }
}

@Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult errorReason) {
    if (errorReason.isUserRecoverableError()) {
        errorReason.getErrorDialog(this, RECOVERY_REQUEST).show();
    } else {
        String error = String.format(getString(R.string.player_error), errorReason.toString());
        Toast.makeText(this, error, Toast.LENGTH_LONG).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RECOVERY_REQUEST) {
        // Retry initialization if user performed a recovery action
        getYouTubePlayerProvider().initialize(Config.YOUTUBE_API_KEY, this);
    }
}

protected Provider getYouTubePlayerProvider() {
    return youTubeView;
}

private void showMessage(String message) {
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}

private final class MyPlaybackEventListener implements YouTubePlayer.PlaybackEventListener {

    @Override
    public void onPlaying() {
        // Called when playback starts, either due to user action or call to play().
        showMessage("Playing");
    }

    @Override
    public void onPaused() {
        // Called when playback is paused, either due to user action or call to pause().
        showMessage("Paused");
    }

    @Override
    public void onStopped() {
        // Called when playback stops for a reason other than being paused.
        showMessage("Stopped");
    }

    @Override
    public void onBuffering(boolean b) {
        // Called when buffering starts or ends.
    }

    @Override
    public void onSeekTo(int i) {
        // Called when a jump in playback position occurs, either
        // due to user scrubbing or call to seekRelativeMillis() or seekToMillis()
    }
}

private final class MyPlayerStateChangeListener implements YouTubePlayer.PlayerStateChangeListener {

    @Override
    public void onLoading() {
        // Called when the player is loading a video
        // At this point, it's not ready to accept commands affecting playback such as play() or pause()
    }

    @Override
    public void onLoaded(String s) {
        // Called when a video is done loading.
        // Playback methods such as play(), pause() or seekToMillis(int) may be called after this callback.
    }

    @Override
    public void onAdStarted() {
        // Called when playback of an advertisement starts.
    }

    @Override
    public void onVideoStarted() {
        // Called when playback of the video starts.
    }

    @Override
    public void onVideoEnded() {
        // Called when the video reaches its end.
    }

    @Override
    public void onError(YouTubePlayer.ErrorReason errorReason) {
        // Called when an error occurs.
    }
}
}

How do I implement picture in picture mode for the video where the video shrinks to the bottom right corner of the app..

Now i get is the youtue video played in the activity.. what i expect is to make the video play in picture-in-picture mode

amit jain
  • 55
  • 1
  • 12

1 Answers1

2

I solved my problem using PictureInPictureParams as follows

First i created a Button (enter_pip) in my xml and in the OnClick i coded the following :

enter_pip.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(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();
            }
        }

    });

I also added the following attributes in my AndroidManifest to my PipActivity to support Picture in Picture in my Activity

<activity
        android:name=".PipActivity"
        android:launchMode="singleTask"
        android:supportsPictureInPicture="true"
        android:theme="@style/AppTheme.NoActionBar" />
amit
  • 709
  • 6
  • 17
amit jain
  • 55
  • 1
  • 12
  • How can i implement this for lower version ? – Snehangshu Kar Feb 05 '19 at 18:37
  • https://medium.com/s23nyc-tech/drop-in-android-video-exoplayer2-with-picture-in-picture-e2d4f8c1eb30 - this link will be of help to you... – amit jain Feb 06 '19 at 07:03
  • I also being able to play the video in youtube player view but video pauses with error Unautorized_overlay when enter in picture in mode. Do you know how to solve this problem? – Gopal Awasthi Mar 22 '20 at 16:18
  • @Gopal Awasthi .. check out this SO link https://stackoverflow.com/questions/16067783/android-youtubeplayer-with-unauthorized-overlay-on-top-of-player – amit Mar 23 '20 at 18:46
  • @amit I have tried all these solutions. Didn't work for me as i am not using any padding. – Gopal Awasthi Mar 24 '20 at 04:13
  • i have posted an answer to your question at https://stackoverflow.com/questions/60808488/unauthorized-overlay-error-on-entering-picture-in-picture-mode-youtubeplayer-vie .. kindly check... – amit Mar 24 '20 at 08:07
  • Any solution? YouTube video plays on a normal screen. However, when I switch to PiP mode. It stops playing with no error. – anil tiwari Feb 24 '21 at 10:55