Skip to content

Commit

Permalink
Move to internal implementation AndroidPlayerSurfaceView
Browse files Browse the repository at this point in the history
  • Loading branch information
StaehliJ committed Feb 5, 2024
1 parent 8fbc890 commit 5bd3267
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 58 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}

0 comments on commit 5bd3267

Please sign in to comment.