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

Media3/ExoPlayer全屏播放器存在屏幕间隙问题求助

问题分析与解决方案

你的Media3 PlayerView与屏幕之间出现间隙,核心原因是Media3对系统窗口Insets的默认适配逻辑以及旧主题属性与新API的兼容性冲突,结合你的代码和版本差异,以下是具体解决方法:

1. 修正主题属性冲突

你当前主题同时设置了android:windowFullscreen和半透明状态栏/导航栏属性,这两个设置存在矛盾:全屏模式下不需要半透明栏,而Media3的PlayerView会自动为半透明栏预留空间,直接导致间隙出现。

修改styles.xml,移除半透明相关属性:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowBackground">@drawable/background_splash</item>
        <!-- 移除冲突的半透明属性 -->
        <!-- <item name="android:windowTranslucentStatus">true</item> -->
        <!-- <item name="android:windowTranslucentNavigation">true</item> -->
    </style>
</resources>

2. 强制PlayerView填充全屏

在布局中给PlayerView添加padding=0并设置resizeMode为填充,确保PlayerView本身无内边距且视频铺满容器:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    tools:context=".view.MainActivity">

    <androidx.media3.ui.PlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="0dp"
        app:use_controller="false"
        app:resizeMode="fill" />
</RelativeLayout>

3. 代码层面强制控制全屏

如果上述方法无效,在Activity的onCreate方法中添加代码,直接隐藏系统栏并清除PlayerView的内边距:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 隐藏状态栏与导航栏
    WindowInsetsController insetsController = getWindow().getInsetsController();
    if (insetsController != null) {
        insetsController.hide(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
        insetsController.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
    }

    // 强制清除PlayerView内边距
    PlayerView playerView = findViewById(R.id.player_view);
    playerView.setPadding(0, 0, 0, 0);
}

补充说明

三年前的ExoPlayer版本对系统Insets的处理逻辑与现在的Media3不同,Media3默认会根据窗口属性自动调整PlayerView布局,这是旧代码失效的核心原因。通过上述步骤可完全消除播放器与屏幕的间隙。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 07:13:17