You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Media3 ExoPlayer横屏模式下控制器位置异常问题求助

解决Jetpack Compose中Media3 ExoPlayer控制器超出播放器区域的问题

问题原因

你的PlayerView布局参数和Compose侧的Modifier约束不匹配,导致控制器布局参考了屏幕全局尺寸,而非你设定的60%宽度的播放器区域:

  • Compose侧用了fillMaxHeight(),未明确限制宽度为60%,播放器实际尺寸超出预期;
  • PlayerView设置了WRAP_CONTENT的布局参数,没有填充Compose分配的容器,控制器无法识别播放器的真实边界。

解决方案

调整Compose Modifier和PlayerView布局参数,明确播放器边界,让控制器自动适配该范围:

1. 修改Compose侧Modifier

用fillMaxWidth(0.6f)明确限制播放器宽度为屏幕的60%,再配合aspectRatio(16/9f)维持宽高比,替代原来的fillMaxHeight()。

2. 修正PlayerView布局参数

将PlayerView的LayoutParams设为MATCH_PARENT,让它完全填充Compose分配的容器空间,确保控制器布局以播放器的真实边界为参考。

修改后的完整代码

AndroidView(
    factory = {
        PlayerView(context).apply {
            useController = true
            resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FILL
            player = exoPlayer
            setShowBuffering(PlayerView.SHOW_BUFFERING_WHEN_PLAYING)
            hideController()
            setKeepContentOnPlayerReset(true)
            setShowNextButton(false)
            setShowPreviousButton(false)
            setFullscreenButtonClickListener {
                isFullscreen = !isFullscreen
                setScreenValue()
                handleFullscreen(isFullscreen, context)
            }
            controllerShowTimeoutMs = 2000
            setControllerVisibilityListener(PlayerView.ControllerVisibilityListener { visibility ->
                isControlVisible = visibility == View.VISIBLE
                val selectedAudioLanguage = exoPlayer.audioFormat?.language
                if (defLanguage != selectedAudioLanguage) {
                    defLanguage = selectedAudioLanguage
                    selectedAudioLanguage?.let { lang ->
                        coroutineScope.launch {
                            userStore.setLanguage(lang)
                        }
                    }
                }
            })
            val progressBar = findViewById<ProgressBar>(R.id.exo_buffering)
            progressBar.indeterminateDrawable.setTint(
                ContextCompat.getColor(context, R.color.travelxp_red),
            )
            // 改为MATCH_PARENT,让PlayerView填充Compose分配的空间
            layoutParams = FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
            )
            setShowSubtitleButton(true)
        }
    }, modifier = Modifier
        .fillMaxWidth(0.6f)
        .aspectRatio(16/9f)
)

补充说明

  • 调整后播放器会严格占据屏幕宽度的60%并保持16:9比例,控制器会自动适配这个边界,不会超出播放器区域;
  • 如果仍有异常,可以检查handleFullscreen方法是否修改了播放器的布局约束,或者尝试自定义控制器布局。

内容的提问来源于stack exchange,提问作者siddhartha verma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 12:42:38