|
| 1 | +package com.pureswift.swiftui |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 4 | +import androidx.compose.foundation.layout.height |
| 5 | +import androidx.compose.runtime.Composable |
| 6 | +import androidx.compose.runtime.DisposableEffect |
| 7 | +import androidx.compose.runtime.remember |
| 8 | +import androidx.compose.ui.platform.LocalContext |
| 9 | +import androidx.compose.ui.viewinterop.AndroidView |
| 10 | +import androidx.compose.ui.unit.dp |
| 11 | +import androidx.media3.common.MediaItem |
| 12 | +import androidx.media3.exoplayer.ExoPlayer |
| 13 | +import androidx.media3.ui.PlayerView |
| 14 | + |
| 15 | +/// Hosts a Media3 player with its standard controls. The player prepares |
| 16 | +/// paused on the first frame; the user starts playback from the controls. |
| 17 | +@Composable |
| 18 | +internal actual fun RenderVideoPlayer(node: ViewNode) { |
| 19 | + val url = node.string("url") ?: return |
| 20 | + val context = LocalContext.current |
| 21 | + val player = remember(node.id, url) { |
| 22 | + ExoPlayer.Builder(context).build().apply { |
| 23 | + setMediaItem(MediaItem.fromUri(url)) |
| 24 | + playWhenReady = false |
| 25 | + prepare() |
| 26 | + } |
| 27 | + } |
| 28 | + DisposableEffect(node.id, url) { |
| 29 | + onDispose { player.release() } |
| 30 | + } |
| 31 | + AndroidView( |
| 32 | + factory = { PlayerView(it).apply { this.player = player } }, |
| 33 | + modifier = node.composeModifiers().fillMaxWidth().height(220.dp), |
| 34 | + ) |
| 35 | +} |
0 commit comments