我正在将视频屏幕从 XML 迁移到 Compose。我需要使用 IMA 插件显示广告。
现在,我正在使用这个依赖项:
implementation "androidx.media3:media3-ui:1.0.0-beta02"
implementation 'androidx.media3:media3-exoplayer:1.0.0-beta02'
implementation "androidx.media3:media3-exoplayer-ima:1.0.0-beta02"
我按如下方式实现播放器:
@Composable
fun VideoPlayer(videoUri: String) {
val context = LocalContext.current
val exoPlayer = remember {
ExoPlayer.Builder(context)
.build()
.apply {
val defaultDataSourceFactory = DefaultDataSource.Factory(context)
val dataSourceFactory: DataSource.Factory = DefaultDataSource.Factory(
context,
defaultDataSourceFactory
)
val source = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(MediaItem.fromUri(videoUri))
setMediaSource(source)
prepare()
}
}
DisposableEffect(
AndroidView(factory = {
PlayerView(context).apply {
hideController()
player = exoPlayer
}
})
) {
onDispose { exoPlayer.release() }
}
}
但是,当我运行我的应用程序并导航到 ExoPlayer 屏幕时,我收到此错误:
java.lang.ClassCastException: com.google.android.exoplayer2.ui.DefaultTimeBar cannot be cast to androidx.media3.ui.TimeBar
我觉得这很奇怪,但也许在 ExoPlayer3 中您可以自定义 TimeBar 并选择要使用的类。有人遇到过这种情况吗?
这是因为您的项目或其依赖项之一依赖于 ExoPlayer 2。您可以尝试查看依赖关系树,看看哪些依赖项依赖于它。
关于查看依赖树,阅读这个问题。