Continue playback of YouTube player while rotating device with separate orientation layouts
Asked Answered
E

1

9

I am trying to add a View containing a YouTube Player to an Activity that keeps on playing when I rotate the device. Since the UI contains more than just the video, I am using a YouTubePlayerFragment.

When the orientation changes from portrait to landscape, the system should use a different layout file. This layout also includes the YouTube player as a view which does not take up the whole screen. Below you will find the bare minimum code to reproduce the problem (for a fresh Android app, minimum API level 19).

package com.example.youtubefragmenttest;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;

public class MainActivity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {

    private static final String YOUTUBE_DEV_KEY = "[your youtube dev key here]";

    private static final String TAG_YOUTUBE_FRAGMENT = "YoutubePlayerFragment";

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

        FragmentManager fm = getFragmentManager();
        YouTubePlayerFragment retainedYoutubeFragment = (YouTubePlayerFragment) fm.findFragmentByTag(TAG_YOUTUBE_FRAGMENT);
        if (retainedYoutubeFragment == null) {
            retainedYoutubeFragment = YouTubePlayerFragment.newInstance();
            retainedYoutubeFragment.setRetainInstance(true);
            FragmentTransaction fragmentTransaction = fm.beginTransaction();
            fragmentTransaction.add(R.id.youtube_fragment, retainedYoutubeFragment, TAG_YOUTUBE_FRAGMENT);
            fragmentTransaction.commit();
        }
        retainedYoutubeFragment.initialize(YOUTUBE_DEV_KEY, this);
    }

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

        if (!wasRestored) {
            youTubePlayer.cueVideo("um4TrbU2Eic");
        }
    }

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

    }
}

Here is the layout file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.youtubefragmenttest.MainActivity">

    <FrameLayout
        android:id="@+id/youtube_fragment"
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

I know the developers of the YouTubePlayer API recommend handling orientation changes manually. This is also the accepted answer in How to retain instance of fragment of fragment playing video after change in orientation?. Unfortunately, this will not work for me because there are too many complex differences between the layouts of portrait view and landscape view.

The documentation hints at a way to fix this such that boolean wasRestored in the callback method onInitializationSuccess is true, but this is the actual problem: wasRestored in the code above is always false.

Bottom line: how do you keep the YouTube player continue playing on orientation change while keeping separate layouts per orientation (which is the main difference with linked question)?

Epigastrium answered 28/6, 2017 at 20:0 Comment(2)
Are you specifically looking for a solution that uses YoutubePlayerFragment? I've done something similar using YoutubeBaseActivity, if you're interested. I use a linear layout that switches orientation from horizontal to vertical, depending on orientation. The video keeps playing as the orientation changes (and when switching to fullscreen as well). I think I took the basic idea from the sample code and then just ran with it. Maybe developers.google.com/youtube/android/player/… can give you some ideas too if you haven't looked at it already.Boycie
I use a YouTubePlayerFragment because the player is a small part of the UI. Furthermore, a YouTubeBaseActivity removes the Action Bar from the UI alongside some other changes to the layout. I already checked the sample apps you mentioned, but the few that have the visual functionality that I desire handle orientation change manually. So unfortunately that is not an option.Epigastrium
W
1

onInitializationSuccess returns a YoutubePlayer object that has a function on getCurrentTimeMillis(). Save this object as an activity instance (this.player = youTubePlayer) inside onInitializationSuccess. Before the activity is destroyed(oPause/onStop), get the current time and pass is to the retained fragment to be stored.

Widera answered 9/7, 2017 at 9:31 Comment(2)
Technically this would work, but it is not an acceptable solution: since the video is reloaded instead of restored, it can cause YouTube ads to start playing again.Epigastrium
Ahhh, that could be a problemWidera

© 2022 - 2024 — McMap. All rights reserved.