我正试图在我的android应用程序中用jetpack编写一个视频。流我使用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
} }
})
发布于 2022-05-03 16:57:12
要使一个应用程序变成全屏,有
with(WindowCompat.getInsetsController(window, window.decorView)) {
systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
hide(WindowInsetsCompat.Type.systemBars())
}
这是我为Kotlin量身定做的,所以你所需要做的就是把它连接到Button
's onClick
中,你就可以开始了。
Button(
onclick = { /*Paste above Code here*/ }
){
Text("Go full-screen") // Whatever here, per your use-case
}
如果由于某种原因不能工作,或者某些东西无法通过onClick
访问,只需创建一个以MutableState<Boolean>
为键的LaunchedEffect
,并更改键以触发该反应。这是不必要的,很可能是因为onClick
应该工作得很好。
发布于 2022-08-30 20:36:06
使用此代码
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)
}
}
}
}
})
对find活动实例使用此扩展函数:
fun Context.findActivity(): Activity? = when (this) {
is Activity -> this
is ContextWrapper -> baseContext.findActivity()
else -> null
}
若要设置屏幕方向,请使用以下扩展函数:
fun Context.setScreenOrientation(orientation: Int) {
val activity = this.findActivity() ?: return
activity.requestedOrientation = orientation
if (orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
hideSystemUi()
} else {
showSystemUi()
}
}
最后,将此函数用于隐藏/显示系统ui (状态栏):
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())
}
发布于 2022-09-16 02:02:28
我有一个用合成媒体实现全屏切换的例子。你不用用这个图书馆。但解决办法是相似的。
MediaContent
,您可以在其中封装全屏切换相关逻辑。下面的代码将类似于YouTube的全屏切换行为(而不是UI)。@Composable
private fun MediaContent(
mediaState: MediaState,
isLandscape: Boolean,
modifier: Modifier = Modifier
) {
val activity = LocalContext.current.findActivity()!!
val enterFullscreen = { activity.requestedOrientation = SCREEN_ORIENTATION_USER_LANDSCAPE }
val exitFullscreen = {
@SuppressLint("SourceLockedOrientationActivity")
// Will reset to SCREEN_ORIENTATION_USER later
activity.requestedOrientation = SCREEN_ORIENTATION_USER_PORTRAIT
}
Box(modifier) {
Media(
mediaState,
modifier = Modifier.fillMaxSize(),
showBuffering = ShowBuffering.Always,
buffering = {
Box(Modifier.fillMaxSize(), Alignment.Center) {
CircularProgressIndicator()
}
},
)
Button(
modifier = Modifier.align(Alignment.BottomEnd),
onClick = if (isLandscape) exitFullscreen else enterFullscreen
) {
Text(text = if (isLandscape) "Exit Fullscreen" else "Enter Fullscreen")
}
}
val onBackPressedCallback = remember {
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
exitFullscreen()
}
}
}
val onBackPressedDispatcher = activity.onBackPressedDispatcher
DisposableEffect(onBackPressedDispatcher) {
onBackPressedDispatcher.addCallback(onBackPressedCallback)
onDispose { onBackPressedCallback.remove() }
}
SideEffect {
onBackPressedCallback.isEnabled = isLandscape
if (isLandscape) {
if (activity.requestedOrientation == SCREEN_ORIENTATION_USER) {
activity.requestedOrientation = SCREEN_ORIENTATION_USER_LANDSCAPE
}
} else {
activity.requestedOrientation = SCREEN_ORIENTATION_USER
}
}
}
@Composable
fun FullscreenToggle() {
val configuration = LocalConfiguration.current
val isLandscape = configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
// hide system ui in fullscreen mode, if you need
val systemUiController = rememberSystemUiController()
SideEffect {
systemUiController.isStatusBarVisible = !isLandscape
systemUiController.isNavigationBarVisible = !isLandscape
}
// create and remember a MediaState instance
val player by rememberManagedExoPlayer()
val mediaState = rememberMediaState(player)
// with movableContentOf, MediaContent's node can be reused between toggling
val mediaContent = remember {
movableContentOf { isLandscape: Boolean, modifier: Modifier ->
MediaContent(mediaState, isLandscape, modifier)
}
}
// this is the UI for normal mode, display the MediaContent with other UI elements
Scaffold(
topBar = {
...
},
modifier = Modifier
.fillMaxSize()
.systemBarsPadding(),
) { padding ->
if (!isLandscape) {
mediaContent(
false,
Modifier
.padding(padding)
.fillMaxWidth()
.aspectRatio(16f / 9f)
)
}
}
// this is the UI for fullscreen mode, only display the MediaContent
if (isLandscape) {
mediaContent(
true,
Modifier
.fillMaxSize()
.background(Color.Black)
)
}
}
https://stackoverflow.com/questions/72102097
复制相似问题