From 87bd9ba585a064bb1ae4de87fb0c32771b9b4778 Mon Sep 17 00:00:00 2001 From: ibaker Date: Wed, 4 Sep 2024 07:21:13 -0700 Subject: [PATCH] Make `PlayerView` Compose workaround opt-in The workaround causes issues with XML-based shared transitions, so we can't apply it unilaterally. Issue: androidx/media#1594 Issue: androidx/media#1237 PiperOrigin-RevId: 670960693 --- RELEASENOTES.md | 7 +++++++ .../java/androidx/media3/ui/PlayerView.java | 20 +++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 0bca1201231..8b3e48a45f9 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -23,6 +23,13 @@ * IMA extension: * Session: * UI: + * Make the stretched/cropped video in + `PlayerView`-in-Compose-`AndroidView` workaround opt-in, due to issues + with XML-based shared transitions. Apps using `PlayerView` inside + `AndroidView` need to call + `PlayerView.setEnableComposeSurfaceSyncWorkaround` in order to opt-in + ([#1237](https://github.com/androidx/media/issues/1237), + [#1594](https://github.com/androidx/media/issues/1594)). * Downloads: * OkHttp Extension: * Cronet Extension: diff --git a/libraries/ui/src/main/java/androidx/media3/ui/PlayerView.java b/libraries/ui/src/main/java/androidx/media3/ui/PlayerView.java index 6e44077aa56..e4c4d0b0135 100644 --- a/libraries/ui/src/main/java/androidx/media3/ui/PlayerView.java +++ b/libraries/ui/src/main/java/androidx/media3/ui/PlayerView.java @@ -333,6 +333,7 @@ public interface FullscreenButtonClickListener { private boolean controllerAutoShow; private boolean controllerHideDuringAds; private boolean controllerHideOnTouch; + private boolean enableComposeSurfaceSyncWorkaround; public PlayerView(Context context) { this(context, /* attrs= */ null); @@ -1304,6 +1305,19 @@ public void setAspectRatioListener( contentFrame.setAspectRatioListener(listener); } + /** + * Whether to enable a workaround for the Compose {@code AndroidView} and {@link SurfaceView} + * compatibility issue described in androidx/media#1237. + * + *

This workaround causes issues with shared element transitions in XML views, so is disabled + * by default (androidx/media#1594). + */ + @UnstableApi + public void setEnableComposeSurfaceSyncWorkaround(boolean enableComposeSurfaceSyncWorkaround) { + this.enableComposeSurfaceSyncWorkaround = enableComposeSurfaceSyncWorkaround; + } + /** * Gets the view onto which video is rendered. This is a: * @@ -1758,7 +1772,7 @@ private void updateAspectRatio() { @Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); - if (Util.SDK_INT == 34 && surfaceSyncGroupV34 != null) { + if (Util.SDK_INT == 34 && surfaceSyncGroupV34 != null && enableComposeSurfaceSyncWorkaround) { surfaceSyncGroupV34.maybeMarkSyncReadyAndClear(); } } @@ -1830,7 +1844,9 @@ public void onVideoSizeChanged(VideoSize videoSize) { @Override public void onSurfaceSizeChanged(int width, int height) { - if (Util.SDK_INT == 34 && surfaceView instanceof SurfaceView) { + if (Util.SDK_INT == 34 + && surfaceView instanceof SurfaceView + && enableComposeSurfaceSyncWorkaround) { // Register a SurfaceSyncGroup to work around https://github.com/androidx/media/issues/1237 // (only present on API 34, fixed on API 35). checkNotNull(surfaceSyncGroupV34)