Compose + Media3:视频播放与 ExoPlayer

2024-06-29 · 24 min · 媒体

Media3 是 Google 推出的新一代媒体库,整合了 ExoPlayer、MediaSession 等组件。

一、基础视频播放器

val exoPlayer = remember {
    ExoPlayer.Builder(context).build().apply {
        setMediaItem(MediaItem.fromUri(videoUrl))
        prepare()
    }
}

AndroidView(
    factory = { ctx ->
        PlayerView(ctx).apply { player = exoPlayer }
    }
)

二、自定义控制器

VideoControls(
    isPlaying = playerState.isPlaying,
    currentPosition = playerState.currentPosition,
    duration = playerState.duration,
    onPlayPause = { ... },
    onSeek = { playerState.seekTo(it) }
)

三、最佳实践

总结