i am trying to steam a video in my android app made with jetpack compose. To stream i using ExoPlayer but i can't really understand how to implement a full screen button, some advice?
@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()
}
}
}
Edit Adding the setControllerOnFullScreenModeChangedListener props exo will show a build in button for the fullscreen, i solved my problem calling the full screen function inside this listner
AndroidView(
factory = { context ->
StyledPlayerView(context).apply {
player = mExoPlayer
setControllerOnFullScreenModeChangedListener {
if(it)
//fullscreen
else
//minimize
} }
})
To make an app go full-screen, there's
with(WindowCompat.getInsetsController(window, window.decorView)) {
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
hide(WindowInsetsCompat.Type.systemBars())
}
Which I've tailored to Kotlin, so all you need to do is just hook it up in a Button
's onClick
and you're good to go.
Button(
onclick = { /*Paste above Code here*/ }
){
Text("Go full-screen") // Whatever here, per your use-case
}
If this does not work for some reason, or something is not accessible through the onClick
, just create a LaunchedEffect
with a MutableState<Boolean>
as the key and change the key to trigger the reaction. Won't be necessary, most probably since the onClick
should work just fine.
use this code
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)
}
}
}
}
})
use this extension function for find activity instance:
fun Context.findActivity(): Activity? = when (this) {
is Activity -> this
is ContextWrapper -> baseContext.findActivity()
else -> null
}
to set screen orientation use below extension function:
fun Context.setScreenOrientation(orientation: Int) {
val activity = this.findActivity() ?: return
activity.requestedOrientation = orientation
if (orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
hideSystemUi()
} else {
showSystemUi()
}
}
and finally use this functions for hide/show system ui (status bar):
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())
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With