zt
11/23/2022, 2:23 AM@Composable
fun Player(
modifier: Modifier = Modifier,
player: Player
) {
AndroidView(
modifier = Modifier
.aspectRatio(16f / 9f)
.then(modifier),
factory = { context ->
SurfaceView(context)
},
update = { surfaceView ->
player.setVideoSurfaceView(surfaceView)
}
)
}
Pablichjenkov
11/23/2022, 4:20 AM@Composable
fun Player(
modifier: Modifier = Modifier,
playerState: IPlayerState
) {
val mediaState = playerState.mediaFlow.collectAsStateWithLifecycle(MediaState.Initializing)
AndroidView(
modifier = Modifier
.aspectRatio(16f / 9f)
.then(modifier),
factory = { context ->
SurfaceView(context)
},
update = { surfaceView ->
when (mediaState) {
Initializing -> {} // Ignore initial state, wait for State to be ready
Streaming -> {
playerState.player.setVideoSurfaceView(surfaceView)
}
Paused -> {
// Show pause UI
}
}
)
}
class PlayerState(player: PlayerController) : IPlayerState {
val mediaFlow = StateFlow<MediaState>
init() {
player.startStreaming()
mediaFlow.value = MediaState.Streaming
}
fun stopVideo() {
player.stopStreaming()
mediaFlow.value = MediaState.Paused
}
}
sealed interface MediaState {
object Initializing: MediaState
object Streaming: MediaState
object Paused: MediaState
...
}
I am assuming a hypothetical player controller class api. My point is to try to pass the State of your composable as an input parameter rather than using remember. The reason is being able to inject it later for test-ability.yschimke
11/23/2022, 8:10 AMyschimke
11/23/2022, 8:11 AMyschimke
11/23/2022, 8:12 AM