Android VideoView-沉浸式-重叠控制器

时间:2019-05-24 23:25:36

标签: java android android-videoview android-immersive

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

  

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

我从上面的网站关注了instruccion,我能够将视频播放器全屏显示,但是我的媒体控制器和设备控制器出现了一个奇怪的问题(它们过大),并且不知道如何解决任何问题有什么问题。

enter image description here

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);
    }
}

3 个答案:

答案 0 :(得分:2)

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

来自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可能会有所帮助。我实际上并未尝试使用此代码,但希望此信息对您有所帮助。

答案 1 :(得分:1)

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

答案 2 :(得分: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> 

VideoPlayer.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);

    }
}