Home » Android » android – YouTubePlayerView reloads the video on screen orientation change

android – YouTubePlayerView reloads the video on screen orientation change

Posted by: admin May 14, 2020 Leave a comment

Questions:

I launch a youtube video from my app using the following activity.
When the activity is launched, youtube video is played. But when I change the orientation of the screen, the video is not resuming but started from the beginning.
What’s the issue here?

package com.example.blahblah;

import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;

public class YTPlayActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {


       static private String yt_sk;
       static private final String DEVELOPER_KEY = "my-api-key";

       @Override

       protected void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);

              setContentView(R.layout.yt_player);
              Bundle extras = getIntent().getExtras();
             yt_sk = extras.getString("yt_sk");

              YouTubePlayerView youTubeView = (YouTubePlayerView)
findViewById(R.id.youtube_view);

           youTubeView.initialize(DEVELOPER_KEY, this);

          }

         @Override

         public void onInitializationFailure(Provider provider,
YouTubeInitializationResult error) {

                Toast.makeText(this, "Oh no! "+error.toString(),
Toast.LENGTH_LONG).show();

       }
       @Override

       public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {

              player.loadVideo(yt_sk);

       }



}
How to&Answers:

Added the configChanges in the AndroidManifest file and it worked.

Eg:

 <activity android:label=Activity Name" 
  android:configChanges="keyboardHidden|orientation|screenSize"
  android:name="com.example.blahblah">