diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerView.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerView.java index 3a1f4f9e35..2842c4440f 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerView.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerView.java @@ -598,6 +598,7 @@ public class PlayerView extends FrameLayout implements AdViewProvider { } else if (surfaceView instanceof SurfaceView) { player.setVideoSurfaceView((SurfaceView) surfaceView); } + updateAspectRatio(); } if (subtitleView != null && player.isCommandAvailable(COMMAND_GET_TEXT)) { subtitleView.setCues(player.getCurrentCues()); @@ -1297,6 +1298,37 @@ public class PlayerView extends FrameLayout implements AdViewProvider { hideArtwork(); } + private void updateAspectRatio() { + VideoSize videoSize = player != null ? player.getVideoSize() : VideoSize.UNKNOWN; + int width = videoSize.width; + int height = videoSize.height; + int unappliedRotationDegrees = videoSize.unappliedRotationDegrees; + float videoAspectRatio = + (height == 0 || width == 0) ? 1 : (width * videoSize.pixelWidthHeightRatio) / height; + + if (surfaceView instanceof TextureView) { + // Try to apply rotation transformation when our surface is a TextureView. + if (unappliedRotationDegrees == 90 || unappliedRotationDegrees == 270) { + // We will apply a rotation 90/270 degree to the output texture of the TextureView. + // In this case, the output video's width and height will be swapped. + videoAspectRatio = 1 / videoAspectRatio; + } + if (textureViewRotation != 0) { + surfaceView.removeOnLayoutChangeListener(componentListener); + } + textureViewRotation = unappliedRotationDegrees; + if (textureViewRotation != 0) { + // The texture view's dimensions might be changed after layout step. + // So add an OnLayoutChangeListener to apply rotation after layout step. + surfaceView.addOnLayoutChangeListener(componentListener); + } + applyTextureViewRotation((TextureView) surfaceView, textureViewRotation); + } + + onContentAspectRatioChanged( + contentFrame, surfaceViewIgnoresVideoAspectRatio ? 0 : videoAspectRatio); + } + @RequiresNonNull("artworkView") private boolean setArtworkFromMetadata(Metadata metadata) { boolean isArtworkSet = false; @@ -1483,33 +1515,7 @@ public class PlayerView extends FrameLayout implements AdViewProvider { @Override public void onVideoSizeChanged(VideoSize videoSize) { - int width = videoSize.width; - int height = videoSize.height; - int unappliedRotationDegrees = videoSize.unappliedRotationDegrees; - float videoAspectRatio = - (height == 0 || width == 0) ? 1 : (width * videoSize.pixelWidthHeightRatio) / height; - - if (surfaceView instanceof TextureView) { - // Try to apply rotation transformation when our surface is a TextureView. - if (unappliedRotationDegrees == 90 || unappliedRotationDegrees == 270) { - // We will apply a rotation 90/270 degree to the output texture of the TextureView. - // In this case, the output video's width and height will be swapped. - videoAspectRatio = 1 / videoAspectRatio; - } - if (textureViewRotation != 0) { - surfaceView.removeOnLayoutChangeListener(this); - } - textureViewRotation = unappliedRotationDegrees; - if (textureViewRotation != 0) { - // The texture view's dimensions might be changed after layout step. - // So add an OnLayoutChangeListener to apply rotation after layout step. - surfaceView.addOnLayoutChangeListener(this); - } - applyTextureViewRotation((TextureView) surfaceView, textureViewRotation); - } - - onContentAspectRatioChanged( - contentFrame, surfaceViewIgnoresVideoAspectRatio ? 0 : videoAspectRatio); + updateAspectRatio(); } @Override diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerView.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerView.java index 311fd0ab03..16ef0894e5 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerView.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerView.java @@ -604,6 +604,7 @@ public class StyledPlayerView extends FrameLayout implements AdViewProvider { } else if (surfaceView instanceof SurfaceView) { player.setVideoSurfaceView((SurfaceView) surfaceView); } + updateAspectRatio(); } if (subtitleView != null && player.isCommandAvailable(COMMAND_GET_TEXT)) { subtitleView.setCues(player.getCurrentCues()); @@ -1448,6 +1449,37 @@ public class StyledPlayerView extends FrameLayout implements AdViewProvider { } } + private void updateAspectRatio() { + VideoSize videoSize = player != null ? player.getVideoSize() : VideoSize.UNKNOWN; + int width = videoSize.width; + int height = videoSize.height; + int unappliedRotationDegrees = videoSize.unappliedRotationDegrees; + float videoAspectRatio = + (height == 0 || width == 0) ? 1 : (width * videoSize.pixelWidthHeightRatio) / height; + + if (surfaceView instanceof TextureView) { + // Try to apply rotation transformation when our surface is a TextureView. + if (unappliedRotationDegrees == 90 || unappliedRotationDegrees == 270) { + // We will apply a rotation 90/270 degree to the output texture of the TextureView. + // In this case, the output video's width and height will be swapped. + videoAspectRatio = 1 / videoAspectRatio; + } + if (textureViewRotation != 0) { + surfaceView.removeOnLayoutChangeListener(componentListener); + } + textureViewRotation = unappliedRotationDegrees; + if (textureViewRotation != 0) { + // The texture view's dimensions might be changed after layout step. + // So add an OnLayoutChangeListener to apply rotation after layout step. + surfaceView.addOnLayoutChangeListener(componentListener); + } + applyTextureViewRotation((TextureView) surfaceView, textureViewRotation); + } + + onContentAspectRatioChanged( + contentFrame, surfaceViewIgnoresVideoAspectRatio ? 0 : videoAspectRatio); + } + @RequiresApi(23) private static void configureEditModeLogoV23(Resources resources, ImageView logo) { logo.setImageDrawable(resources.getDrawable(R.drawable.exo_edit_mode_logo, null)); @@ -1524,33 +1556,7 @@ public class StyledPlayerView extends FrameLayout implements AdViewProvider { @Override public void onVideoSizeChanged(VideoSize videoSize) { - int width = videoSize.width; - int height = videoSize.height; - int unappliedRotationDegrees = videoSize.unappliedRotationDegrees; - float videoAspectRatio = - (height == 0 || width == 0) ? 1 : (width * videoSize.pixelWidthHeightRatio) / height; - - if (surfaceView instanceof TextureView) { - // Try to apply rotation transformation when our surface is a TextureView. - if (unappliedRotationDegrees == 90 || unappliedRotationDegrees == 270) { - // We will apply a rotation 90/270 degree to the output texture of the TextureView. - // In this case, the output video's width and height will be swapped. - videoAspectRatio = 1 / videoAspectRatio; - } - if (textureViewRotation != 0) { - surfaceView.removeOnLayoutChangeListener(this); - } - textureViewRotation = unappliedRotationDegrees; - if (textureViewRotation != 0) { - // The texture view's dimensions might be changed after layout step. - // So add an OnLayoutChangeListener to apply rotation after layout step. - surfaceView.addOnLayoutChangeListener(this); - } - applyTextureViewRotation((TextureView) surfaceView, textureViewRotation); - } - - onContentAspectRatioChanged( - contentFrame, surfaceViewIgnoresVideoAspectRatio ? 0 : videoAspectRatio); + updateAspectRatio(); } @Override