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)?
YouTubePlayerFragment
because the player is a small part of the UI. Furthermore, aYouTubeBaseActivity
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