首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Jetpack compose Exoplayer全屏

Jetpack compose Exoplayer全屏
EN

Stack Overflow用户
提问于 2022-05-03 15:51:13
回答 3查看 2.1K关注 0票数 1

我正试图在我的android应用程序中用jetpack编写一个视频。流我使用ExoPlayer,但我不能真正理解如何实现一个全屏按钮,一些建议?

代码语言:javascript
运行
复制
@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将显示全屏的内置按钮,我解决了在这个列表中调用全屏函数的问题

代码语言:javascript
运行
复制
            AndroidView(
                factory = { context ->
                    StyledPlayerView(context).apply {
                        player = mExoPlayer
                        setControllerOnFullScreenModeChangedListener {
                        if(it)
                            //fullscreen
                        else
                            //minimize
                    }                    }
                })
EN

回答 3

Stack Overflow用户

发布于 2022-05-03 16:57:12

要使一个应用程序变成全屏,有

代码语言:javascript
运行
复制
with(WindowCompat.getInsetsController(window, window.decorView)) {
    systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    hide(WindowInsetsCompat.Type.systemBars())
}

这是我为Kotlin量身定做的,所以你所需要做的就是把它连接到Button's onClick中,你就可以开始了。

代码语言:javascript
运行
复制
Button(
  onclick = { /*Paste above Code here*/ }
){
 Text("Go full-screen") // Whatever here, per your use-case
}

如果由于某种原因不能工作,或者某些东西无法通过onClick访问,只需创建一个以MutableState<Boolean>为键的LaunchedEffect,并更改键以触发该反应。这是不必要的,很可能是因为onClick应该工作得很好。

票数 6
EN

Stack Overflow用户

发布于 2022-08-30 20:36:06

使用此代码

代码语言:javascript
运行
复制
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活动实例使用此扩展函数:

代码语言:javascript
运行
复制
fun Context.findActivity(): Activity? = when (this) {
    is Activity       -> this
    is ContextWrapper -> baseContext.findActivity()
    else              -> null
}

若要设置屏幕方向,请使用以下扩展函数:

代码语言:javascript
运行
复制
fun Context.setScreenOrientation(orientation: Int) {
    val activity = this.findActivity() ?: return
    activity.requestedOrientation = orientation
    if (orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
       hideSystemUi()
    } else {
       showSystemUi()
    }
}

最后,将此函数用于隐藏/显示系统ui (状态栏):

代码语言:javascript
运行
复制
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())
}
票数 1
EN

Stack Overflow用户

发布于 2022-09-16 02:02:28

我有一个用合成媒体实现全屏切换的例子。你不用用这个图书馆。但解决办法是相似的。

  1. 为媒体回放实现一个可组合的MediaContent,您可以在其中封装全屏切换相关逻辑。下面的代码将类似于YouTube的全屏切换行为(而不是UI)。
代码语言:javascript
运行
复制
@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
        }
    }
}
  1. 然后你可以使用它:
代码语言:javascript
运行
复制
@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://github.com/fengdai/compose-media/blob/master/sample/src/main/java/com/github/fengdai/compose/media/sample/FullscreenToggle.kt

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72102097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档