1

I am using YouTubePlayerView to load the video.The video plays successfully, but when the screen is rotated, the it stops playing.How do I overcome this problem?It may be a simple thing.I googled but didn't get help but your help is needed... Below is my code.

Code:

youTubePlayerView=findViewById(R.id.youtube_view);
        onInitializedListener=new YouTubePlayer.OnInitializedListener() {
            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                youTubePlayers=youTubePlayer;
                youTubePlayers.loadVideo("9ZaEPeaucIU");
                youTubePlayers.setFullscreenControlFlags(FULLSCREEN_FLAG_CONTROL_ORIENTATION);
                youTubePlayers.setOnFullscreenListener(onFullscreenListener);
            }
            @Override
            public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                }
        };
        onFullscreenListener=new YouTubePlayer.OnFullscreenListener() {
            @Override
            public void onFullscreen(boolean b) {
                if (b){
                    time=youTubePlayers.getCurrentTimeMillis();


                }

            }
        };
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                youTubePlayerView.initialize("AI&*$^$^$3634HxbhVM@#$%%^%&%$",onInitializedListener);

                }
        });

Layout:

<com.google.android.youtube.player.YouTubePlayerView
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:id="@+id/youtube_view"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent">
</com.google.android.youtube.player.YouTubePlayerView>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
jobin
  • 1,489
  • 5
  • 27
  • 52
  • Try `youTubePlayers.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);` or `youTubePlayers.addFullscreenControlFlag(FULLSCREEN_FLAG_CONTROL_ORIENTATION);` in `onInitializationSuccess()` – Abbas Dec 07 '18 at 08:36
  • I have already done that.It is in the code..Please check it – jobin Dec 07 '18 at 08:42
  • Try the `addFullscreenControlFlag()` instead of `setFullscreenControlFlag()` – Abbas Dec 07 '18 at 08:54
  • No..it didnt work... – jobin Dec 07 '18 at 09:10

2 Answers2

2

Add this on your activity declaration in AndroidManifext.xml so that, the activtiy doesn't recreate on orientation change.

<activity
        android:name=".NameOfYourActivitiy"
        android:configChanges="orientation|screenSize">

</activity>
touhid udoy
  • 4,005
  • 2
  • 18
  • 31
1

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">