@@ -87,6 +87,8 @@ public final class MainPlayerUi extends VideoPlayerUi implements View.OnLayoutCh
8787 private static final int DETAIL_TITLE_TEXT_SIZE_TV = 16 ; // sp
8888 private static final int DETAIL_TITLE_TEXT_SIZE_TABLET = 15 ; // sp
8989
90+ private boolean portraitBefore = false ;
91+ private boolean isOnScreenRotationButtonClicked = false ;
9092 private boolean isFullscreen = false ;
9193 private boolean isVerticalVideo = false ;
9294 private boolean fragmentIsVisible = false ;
@@ -138,6 +140,9 @@ public void setupAfterIntent() {
138140 // Android TV: without it focus will frame the whole player
139141 binding .playPauseButton .requestFocus ();
140142
143+ // Tracks the orientation if it's portrait. See #13057
144+ portraitBefore = !isLandscape ();
145+
141146 // Note: This is for automatically playing (when "Resume playback" is off), see #6179
142147 if (player .getPlayWhenReady ()) {
143148 player .play ();
@@ -158,6 +163,7 @@ protected void initListeners() {
158163 binding .screenRotationButton .setOnClickListener (makeOnClickListener (() -> {
159164 // Only if it's not a vertical video or vertical video but in landscape with locked
160165 // orientation a screen orientation can be changed automatically
166+ isOnScreenRotationButtonClicked = true ;
161167 if (!isVerticalVideo || (isLandscape () && globalScreenOrientationLocked (context ))) {
162168 player .getFragmentListener ()
163169 .ifPresent (PlayerServiceEventListener ::onScreenRotationButtonClicked );
@@ -235,7 +241,6 @@ public void removeViewFromParent() {
235241 @ Override
236242 public void destroy () {
237243 super .destroy ();
238-
239244 // Exit from fullscreen when user closes the player via notification
240245 if (isFullscreen ) {
241246 toggleFullscreen ();
@@ -920,6 +925,18 @@ public void toggleFullscreen() {
920925 }
921926
922927 isFullscreen = !isFullscreen ;
928+
929+ // If toggleFullscreen was called without the fullscreen button clicked,
930+ // then it means the user changed the orientation of the device to landscape,
931+ // and portraitBefore should be false.
932+ // If the fullscreen button was clicked and it's no longer in fullscreen,
933+ // then portraitBefore should be set depending on the orientation.
934+ // See #13057 and below.
935+ if ((!isOnScreenRotationButtonClicked || !isFullscreen )
936+ && player .getCurrentState () != STATE_COMPLETED ) {
937+ portraitBefore = !isLandscape ();
938+ }
939+
923940 if (isFullscreen ) {
924941 // Android needs tens milliseconds to send new insets but a user is able to see
925942 // how controls changes it's position from `0` to `nav bar height` padding.
@@ -930,11 +947,27 @@ public void toggleFullscreen() {
930947 // from landscape to portrait (open vertical video to reproduce)
931948 binding .playbackControlRoot .setPadding (0 , 0 , 0 , 0 );
932949 }
950+
951+ // When the video ends, the orientation remains in landscape
952+ // although it was in portrait when fullscreen was clicked.
953+ // This corrects the orientation back to portrait. See #13057.
954+ if (player .getCurrentState () == STATE_COMPLETED
955+ && !DeviceUtils .isTablet (context )
956+ && !DeviceUtils .isTv (context )
957+ && portraitBefore
958+ && !isVerticalVideo
959+ && isLandscape ()
960+ && globalScreenOrientationLocked (context )) {
961+ fragmentListener .onScreenRotationButtonClicked ();
962+ }
963+
933964 fragmentListener .onFullscreenStateChanged (isFullscreen );
934965
935966 binding .metadataView .setVisibility (isFullscreen ? View .VISIBLE : View .GONE );
936967 binding .playerCloseButton .setVisibility (isFullscreen ? View .GONE : View .VISIBLE );
937968 setupScreenRotationButton ();
969+
970+ isOnScreenRotationButtonClicked = false ;
938971 }
939972
940973 public void checkLandscape () {
0 commit comments