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)?
From there, go down and click on YouTube Settings. Select “Playback.” On the left hand side of the new page, you will see the “Account Settings” menu. Select ‘Playback’ option from the list below, second from the bottom. Adjust the Playback settings. From here, you can adjust a number of things.
When you are logged in and on the home screen, you will need to click on the gear symbol on the top right of your screen. From there, go down and click on YouTube Settings. Select "Playback." On the left hand side of the new page, you will see the "Account Settings" menu. Select ‘Playback’ option from the list below, second from the bottom.
YouTube allows up to 4K resolution to steam the video. Usually, it’s set at auto to stream the video based on available internet speed. But if you are manually adjusting the video resolution to higher resolution and don’t have internet bandwidth to load the video, then it might throw playback error after continuous loading.
If there is no physical Home button, swipe down from the top-right corner instead. Tap the Rotate Lock button. By default, your iPhone or iPad will automatically rotate your YouTube video when you turn the phone or tablet to the side or upside-down. This won't happen if you turn on Rotate Lock.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With