Videoview 未在 nougat 24 中显示,但在 android Q 29 中显示和工作

Videoview is not displaying in nougat 24 but displaying and working in android Q 29

我正在申请。单击按钮时,它会显示 window 和 videoview 的弹出窗口。它显示在 android 29 api 中,但未显示在 android 24 api 中。下面是showvideopopup函数的代码。

private void showVideoPopup()
    {
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View customView = layoutInflater.inflate(R.layout.dispensing_intro,null);

        int uiOptions =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
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        customView.setSystemUiVisibility(uiOptions);


//        closePopupBtn = (Button) customView.findViewById(R.id.closeView);

        //instantiate popup window
        dispense_intro_popup = new PopupWindow(customView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        //display the popup window
        dispense_intro_popup.showAtLocation(dispense_intro_view, Gravity.CENTER, 0, 0);

        VideoView video = (VideoView) customView.findViewById(R.id.dispense_intro);
        Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/raw/"+ R.raw.dispense_intro);
        video.setVideoURI(videoUri);
        video.start();

        video.setOnCompletionListener(this);

    }

以下是popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:background="#b3000000"
    android:layout_height="700dp"
    android:id="@+id/dispense_intro_holder"
    android:layout_gravity="center"
    android:gravity="center">


    <VideoView
        android:id="@+id/dispense_intro"
        android:layout_gravity="center"
        android:layout_width="640dp"
        android:layout_height="360dp"  />
</LinearLayout>

使用 DialogFragment() 而不是 PopupWindow

然后将以下代码添加到片段onCreateView

PopupFragment.java

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.popup, container, false);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.FullscreenTheme);
        VideoView video = (VideoView) rootView.findViewById(R.id.dispense_intro);
        Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/raw/"+ R.raw.dispense_intro);
        video.setVideoURI(videoUri);
        video.start();

        video.setOnCompletionListener(this);

        return rootView;

}

并使对话框片段全屏使用以下代码 onDialogCreated

public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog.getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE
                        | 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);
        return dialog;
    }