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

Android 13设备ExoPlayer旋转后无法正常横屏全屏求助

Android 13 下 ExoPlayer 旋转全屏异常修复方案

核心问题分析

Android 13 对屏幕方向控制新增了更严格的权限限制,单纯靠 Manifest 配置已经无法完全生效,必须结合代码动态申请权限+处理配置变更才能解决。

具体修复步骤

1. 动态申请屏幕方向控制权限

Android 13 引入了 REQUEST_SCREEN_ORIENTATION 权限,需在代码中主动申请:

// 在VideoPlayerActivity的onCreate或进入全屏前调用
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
    requestPermissions(arrayOf(Manifest.permission.REQUEST_SCREEN_ORIENTATION), 1001)
}

2. 修正 Manifest 配置

保留配置变更监听,移除强制方向的固定配置(避免冲突):

<activity
    android:name=".VideoPlayerActivity"
    android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout"
    android:exported="true" />

3. 代码中动态切换屏幕方向

在触发全屏的逻辑(设备旋转监听、全屏按钮点击)中,主动设置方向:

// 进入全屏时切换为横屏
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
// 退出全屏时恢复竖屏
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT

4. 处理配置变更回调

重写方法确保ExoPlayer视图适配新尺寸:

override fun onConfigurationChanged(newConfig: Configuration) {
    super.onConfigurationChanged(newConfig)
    val playerView = findViewById<PlayerView>(R.id.player_view)
    // 强制视图填充屏幕
    playerView.layoutParams = LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT
    )
    playerView.requestLayout()
}

5. 检查PlayerView配置

确保视图的缩放模式设置为填充:

<com.google.android.exoplayer2.ui.PlayerView
    android:id="@+id/player_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:resize_mode="fill" />

额外注意事项

  • 测试时需关闭再打开系统"自动旋转"开关,确保权限生效
  • 若使用ExoPlayer默认全屏按钮,建议自定义全屏逻辑替换默认行为,避免权限冲突

内容的提问来源于stack exchange,提问作者Merlin Jeyakumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 17:46:01