我正在尝试在使用Jetpack Compose制作的Android应用中播放视频。为了进行流媒体传输,我使用ExoPlayer,但我无法真正理解如何实现全屏按钮,有什么建议吗?
@Composable
private fun VideoPlayer() {
val videoURI = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
val httpDataSourceFactory: HttpDataSource.Factory =
DefaultHttpDataSource.Factory().setAllowCrossProtocolRedirects(false)
val dataSourceFactory: DataSource.Factory = DataSource.Factory {
val dataSource = httpDataSourceFactory.createDataSource()
dataSource.setRequestProperty(
"cookie", "cookieValue"
)
dataSource.setRequestProperty("Range", "1-10000")
dataSource
}
val mContext = LocalContext.current
// Initializing ExoPLayer
val mExoPlayer = remember(mContext) {
ExoPlayer.Builder(mContext)
.setMediaSourceFactory(DefaultMediaSourceFactory(dataSourceFactory)).build().apply {
val mediaItem = MediaItem.Builder()
.setUri(Uri.parse(videoURI))
.build()
setMediaItem(mediaItem)
playWhenReady = true
prepare()
}
}
DisposableEffect(
// Implementing ExoPlayer
AndroidView(factory = { context ->
StyledPlayerView(context).apply {
player = mExoPlayer
}
})
) {
onDispose {
mExoPlayer.release()
}
}
}
编辑 添加setControllerOnFullScreenModeChangedListener属性,exo将显示一个内置的全屏按钮,我通过在此侦听器中调用全屏函数解决了我的问题。
AndroidView(
factory = { context ->
StyledPlayerView(context).apply {
player = mExoPlayer
setControllerOnFullScreenModeChangedListener {
if(it)
//fullscreen
else
//minimize
} }
})