From 5bd3267e736989d195b6389836089557f6979ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaquim=20St=C3=A4hli?= Date: Mon, 5 Feb 2024 09:54:58 +0100 Subject: [PATCH] Move to internal implementation AndroidPlayerSurfaceView --- .../widget/player/AndroidPlayerSurfaceView.kt | 58 ------------------- .../ui/widget/player/PlayerSurface.kt | 48 +++++++++++++++ 2 files changed, 48 insertions(+), 58 deletions(-) delete mode 100644 pillarbox-ui/src/main/java/ch/srgssr/pillarbox/ui/widget/player/AndroidPlayerSurfaceView.kt diff --git a/pillarbox-ui/src/main/java/ch/srgssr/pillarbox/ui/widget/player/AndroidPlayerSurfaceView.kt b/pillarbox-ui/src/main/java/ch/srgssr/pillarbox/ui/widget/player/AndroidPlayerSurfaceView.kt deleted file mode 100644 index 3dd084ac1..000000000 --- a/pillarbox-ui/src/main/java/ch/srgssr/pillarbox/ui/widget/player/AndroidPlayerSurfaceView.kt +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) SRG SSR. All rights reserved. - * License information is available from the LICENSE file. - */ -package ch.srgssr.pillarbox.ui.widget.player - -import android.content.Context -import android.view.SurfaceView -import androidx.compose.runtime.Composable -import androidx.compose.ui.Modifier -import androidx.compose.ui.draw.clipToBounds -import androidx.compose.ui.viewinterop.AndroidView -import androidx.media3.common.Player - -/** - * Render [player] content on a [SurfaceView] - * - * @param player The player to render on the SurfaceView. - * @param modifier The modifier to be applied to the layout. - */ -@Composable -fun AndroidPlayerSurfaceView(player: Player, modifier: Modifier = Modifier) { - AndroidView( - /* - * On some devices (Pixel 2 XL Android 11) - * the "black" background of the SurfaceView shows outside its bound. - */ - modifier = modifier.clipToBounds(), - factory = { context -> - PlayerSurfaceView(context) - }, update = { view -> - view.player = player - }, onRelease = { view -> - view.player = null - }, onReset = { view -> - // onRested is called before update when composable is reuse with different context. - view.player = null - } - ) -} - -/** - * Player surface view - */ -internal class PlayerSurfaceView(context: Context) : SurfaceView(context) { - - /** - * Player if null is passed just clear surface - */ - var player: Player? = null - set(value) { - if (field != value) { - field?.clearVideoSurfaceView(this) - value?.setVideoSurfaceView(this) - } - field = value - } -} diff --git a/pillarbox-ui/src/main/java/ch/srgssr/pillarbox/ui/widget/player/PlayerSurface.kt b/pillarbox-ui/src/main/java/ch/srgssr/pillarbox/ui/widget/player/PlayerSurface.kt index aa2ed3279..49bee5061 100644 --- a/pillarbox-ui/src/main/java/ch/srgssr/pillarbox/ui/widget/player/PlayerSurface.kt +++ b/pillarbox-ui/src/main/java/ch/srgssr/pillarbox/ui/widget/player/PlayerSurface.kt @@ -4,6 +4,8 @@ */ package ch.srgssr.pillarbox.ui.widget.player +import android.content.Context +import android.view.SurfaceView import androidx.compose.foundation.Canvas import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.BoxScope @@ -20,6 +22,7 @@ import androidx.compose.ui.draw.clipToBounds import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.drawscope.Stroke +import androidx.compose.ui.viewinterop.AndroidView import androidx.media3.common.Player import ch.srgssr.pillarbox.ui.ScaleMode import ch.srgssr.pillarbox.ui.exoplayer.ExoPlayerSubtitleView @@ -126,3 +129,48 @@ fun DebugPlayerView(modifier: Modifier) { ) } } + +/** + * Render [player] content on a [SurfaceView] + * + * @param player The player to render on the SurfaceView. + * @param modifier The modifier to be applied to the layout. + */ +@Composable +internal fun AndroidPlayerSurfaceView(player: Player, modifier: Modifier = Modifier) { + AndroidView( + /* + * On some devices (Pixel 2 XL Android 11) + * the "black" background of the SurfaceView shows outside its bound. + */ + modifier = modifier.clipToBounds(), + factory = { context -> + PlayerSurfaceView(context) + }, update = { view -> + view.player = player + }, onRelease = { view -> + view.player = null + }, onReset = { view -> + // onRested is called before update when composable is reuse with different context. + view.player = null + } + ) +} + +/** + * Player surface view + */ +internal class PlayerSurfaceView(context: Context) : SurfaceView(context) { + + /** + * Player if null is passed just clear surface + */ + var player: Player? = null + set(value) { + if (field != value) { + field?.clearVideoSurfaceView(this) + value?.setVideoSurfaceView(this) + } + field = value + } +}