首页 > 解决方案 > 如何将 VideoView MediaController 添加到底部工作表对话框

问题描述

我正在尝试在底部工作表中添加 Videoview,在框架布局中添加 Videoview,我的问题是 Mediacontroller 没有出现在框架内,它出现在活动中(在底部工作表对话框后面)

底部片段.xml

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:layout_margin="10dp"
        android:background="@drawable/bottom_sheet_bg"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_video"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="Video" />

        <FrameLayout
            android:id="@+id/frame_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:layout_marginStart="5dp"
            android:layout_below="@+id/tv_video">
            <VideoView
                android:id="@+id/vv_info_video"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </FrameLayout>

    </RelativeLayout>

</RelativeLayout>

bottomsheetfragment.java

public class BottomSheetFragment extends BottomSheetDialogFragment {

    private static final String TAG = "BottomSheetFragment";

    MainActivity activity;
    View view;
    public static BottomSheetFragment newInstance() {
        BottomSheetFragment fragment = new BottomSheetFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d(TAG, "onCreate: ");
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         super.onCreateView(inflater, container, savedInstanceState);

        view = View.inflate(getContext(), R.layout.fragment_bottom_sheet, null);
        view.setBackgroundColor(getResources().getColor(android.R.color.transparent));

        try{
            VideoView videoView = view.findViewById(R.id.vv_info_video);
            String videoPath = "android.resource://"+requireActivity().getPackageName()+"/"+R.raw.myvideo;
            Uri uri = Uri.parse(videoPath);
            videoView.setVideoURI(uri);

            MediaController mediaController = new MediaController(getContext());
            mediaController.setAnchorView(videoView);
            mediaController.setMediaPlayer(videoView);
            mediaController.setVisibility(View.VISIBLE);
            mediaController.show();

            videoView.setMediaController(mediaController);
            videoView.start();

        }catch (Exception e){
            GlobalFunctions.printException(e);
        }

        return view;
    }

    @Override
    public void setupDialog(Dialog dialog, int style) {
        Log.d(TAG, "setupDialog: ");
        //dialog.setContentView(view);
    }

}

我从片段内的按钮调用这个片段

private void showBottomSheetInfo(){
        BottomSheetFragment bottomSheetDialog = BottomSheetFragment.newInstance();
        bottomSheetDialog.show(requireActivity().getSupportFragmentManager(), "Bottom Sheet Dialog Fragment");
    }

标签: javaandroid

解决方案


推荐阅读