7

我的应用程序上有一个视频视图,我使用 gogole 作为参考来使视频播放器全屏显示:

https://developer.android.com/training/system-ui/immersive#java

我按照上述网站上的说明进行操作,并且能够使视频播放器全屏显示,但是我的媒体控制器和设备控制器出现了一个奇怪的问题(它们重叠)并且不知道如何解决任何想法问题。

在此处输入图像描述

public class VideoPlayer extends AppCompatActivity {

    VideoView videoView;
    MediaController mediaController;
    private String TAG = VideoPlayer.class.getSimpleName();
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_player_activity);

        videoView = (VideoView) findViewById(R.id.videoView);
        mediaController = new MediaController(this);
        videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.video1);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);
        videoView.start(); 

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            hideSystemUI();
        }
    }

    private void hideSystemUI() { 
        View decorView = getWindow().getDecorView();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                            | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    } 
    private void showSystemUI() {
        Log.e(TAG, "111");
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }
}
4

3 回答 3

1

您可以使用 google exoplayer自定义布局。您可以通过设计自定义布局来添加您的自定义布局。它非常简单,由google exoplayer库提供。请参阅下面的示例链接: https ://exoplayer.dev/ui-组件.html

于 2019-06-03T10:18:21.250 回答
1

这是为您解决的问题。您必须通过代码动态更新控件边距。想法是在布局文件中添加一个带有控件的叠加层。并且当导航栏可见时,更新您的控件底部边距以将它们向上推到屏幕上。休息代码解释它对我有用。

布局文件:video_player_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <VideoView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/videoView"/>

    <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content"
              android:text="Video Controls" android:textStyle="bold" android:textSize="40sp"
              android:textColor="#FFF"
              android:background="#444444"
              android:gravity="center"
              android:layout_alignParentBottom="true"/>

    <View android:visibility="gone" android:layout_width="match_parent" android:layout_height="match_parent"
          android:id="@+id/overlay"/>

</RelativeLayout> 

视频播放器.java

package com.vpiitsolution.stack;

import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import static android.view.View.SYSTEM_UI_FLAG_FULLSCREEN;

public class VideoPlayer extends AppCompatActivity {

    VideoView videoView;
    MediaController mediaController;
    private String TAG = VideoPlayer.class.getSimpleName();
    private View overlay;

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_player_activity);

        videoView = findViewById(R.id.videoView);
        mediaController = new MediaController(this);
        //videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.video1);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);
        videoView.start();
        hideSystemUI();
        overlay = findViewById(R.id.overlay);
        overlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideSystemUI();
            }
        });
        videoView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
                    updateControls(getNavigationBarHeight());
                    overlay.setVisibility(View.VISIBLE);
                } else {
                    updateControls(0);
                    overlay.setVisibility(View.GONE);
                }

            }
        });

    }

    int getNavigationBarHeight() {
        Resources resources = getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return resources.getDimensionPixelSize(resourceId);
        }
        return 0;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            hideSystemUI();
        } else {
            showSystemUI();
        }

    }

    private void hideSystemUI() {
        View decorView = getWindow().getDecorView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                            | SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                            | View.SYSTEM_UI_FLAG_IMMERSIVE);
        }
    }

    void updateControls(int bottomMargin) {
        TextView tv = findViewById(R.id.text);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tv.getLayoutParams();
        params.bottomMargin = bottomMargin;
        tv.setLayoutParams(params);
    }

   /* private void hideSystemUI() {
        // Enables regular immersive mode.
        // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
        // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE
                        // Set the content to appear under the system bars so that the
                        // content doesn't resize when the system bars hide and show.
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        // Hide the nav bar and status bar
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);
    }*/

    private void showSystemUI() {
        Log.e(TAG, "111");
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

    }
}
于 2019-06-04T08:59:38.087 回答
1

我认为可能会发生以下情况:

来自https://developer.android.com/training/system-ui/immersive#EnableFullscreen

注意:当您使用 SYSTEM_UI_FLAG_IMMERSIVE_STICKY 标志时,滑动会导致系统 UI > 暂时以半透明状态出现,但不会清除任何标志并且不会触发您的系统 UI 可见性更改侦听器。

来自https://developer.android.com/reference/android/view/View.html#SYSTEM_UI_FLAG_IMMERSIVE_STICKY

当系统栏在沉浸式模式下隐藏时,可以通过系统手势(例如从屏幕顶部滑动)临时显示它们。这些瞬态系统栏会覆盖应用程序的内容,可能具有一定程度的透明度,并会在短暂超时后自动隐藏。

来自https://developer.android.com/training/system-ui/immersive#leanback

向后倾斜模式适用于全屏体验,其中用户不会与屏幕进行大量交互,例如在观看视频时。

当用户想要恢复系统栏时,他们只需在任意位置点击屏幕即可。

要启用倾斜模式,请调用 setSystemUiVisibility() 并传递 SYSTEM_UI_FLAG_FULLSCREEN 和 SYSTEM_UI_FLAG_HIDE_NAVIGATION。

根据前面的信息,我认为删除 SYSTEM_UI_FLAG_IMMERSIVE_STICKY 可能会有所帮助。我实际上并没有尝试使用此代码,但我希望这些信息有所帮助。

于 2019-05-30T00:51:18.920 回答