Ans*_*sol 3 exoplayer android-jetpack-compose
我正在尝试在我的 Android 应用程序中使用 jetpack compose 制作视频。要使用 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()
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑 添加 setControllerOnFullScreenModeChangedListener 道具 exo 将显示全屏的内置按钮,我解决了调用此监听器中的全屏功能的问题
AndroidView(
factory = { context ->
StyledPlayerView(context).apply {
player = mExoPlayer
setControllerOnFullScreenModeChangedListener {
if(it)
//fullscreen
else
//minimize
} }
})
Run Code Online (Sandbox Code Playgroud)
小智 8
使用此代码
AndroidView(factory = {
StyledPlayerView(context).apply {
player = exoPlayer
setFullscreenButtonClickListener { isFullScreen ->
with(context) {
if (isFullScreen) {
setScreenOrientation(orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
} else {
setScreenOrientation(orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
}
}
}
}
})
Run Code Online (Sandbox Code Playgroud)
使用此扩展函数来查找活动实例:
fun Context.findActivity(): Activity? = when (this) {
is Activity -> this
is ContextWrapper -> baseContext.findActivity()
else -> null
}
Run Code Online (Sandbox Code Playgroud)
要设置屏幕方向,请使用以下扩展功能:
fun Context.setScreenOrientation(orientation: Int) {
val activity = this.findActivity() ?: return
activity.requestedOrientation = orientation
if (orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
hideSystemUi()
} else {
showSystemUi()
}
}
Run Code Online (Sandbox Code Playgroud)
最后使用此函数隐藏/显示系统用户界面(状态栏):
fun Context.hideSystemUi() {
val activity = this.findActivity() ?: return
val window = activity.window ?: return
WindowCompat.setDecorFitsSystemWindows(window, false)
WindowInsetsControllerCompat(window, window.decorView).let { controller ->
controller.hide(WindowInsetsCompat.Type.systemBars())
controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
}
}
fun Context.showSystemUi() {
val activity = this.findActivity() ?: return
val window = activity.window ?: return
WindowCompat.setDecorFitsSystemWindows(window, true)
WindowInsetsControllerCompat(
window,
window.decorView
).show(WindowInsetsCompat.Type.systemBars())
}
Run Code Online (Sandbox Code Playgroud)
要使应用程序全屏显示,有
with(WindowCompat.getInsetsController(window, window.decorView)) {
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
hide(WindowInsetsCompat.Type.systemBars())
}
Run Code Online (Sandbox Code Playgroud)
这是我为 Kotlin 定制的,所以您所需要做的就是将其连接到 a 中Button,onClick然后就可以开始了。
Button(
onclick = { /*Paste above Code here*/ }
){
Text("Go full-screen") // Whatever here, per your use-case
}
Run Code Online (Sandbox Code Playgroud)
如果由于某种原因这不起作用,或者无法通过 访问某些内容onClick,只需创建一个LaunchedEffect以 aMutableState<Boolean>作为键并更改键即可触发反应。没有必要,很可能是因为onClick应该可以正常工作。
| 归档时间: |
|
| 查看次数: |
7421 次 |
| 最近记录: |