PlayerView sets aspect ratio when setting a new player

PlayerView and StyledPlayerView handled this in
onVideoSizeChanged but it can be omitted.
(e.g. if the player is MediaController)

PiperOrigin-RevId: 382340927
This commit is contained in:
klhyun 2021-06-30 18:56:53 +01:00 committed by kim-vde
parent d9d02710c0
commit 3c97815d55
2 changed files with 66 additions and 54 deletions

View File

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

View File

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