问题描述
我想将视频加载到高度为140dp的BottomSheetDialogFragment中的VideoView中,但是当我打开对话框时,视频将在屏幕顶部播放
BottomSheetDialogFragment:
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="140dp" />
<TextView
android:id="@+id/txt_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_gravity="center"
android:layout_marginTop="23dp"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:textColor="@color/black"
android:letterSpacing="0.01"
android:text="dummy_large_text"
android:linespacingExtra="6.5sp"
tools:ignore="RtlSymmetry"
tools:targetApi="lollipop" />
<Button
android:id="@+id/btn_back"
android:layout_width="match_parent"
android:layout_height="60.2dp"
android:textColor="@color/white"
android:layout_marginTop="21.5dp"
android:text="@string/back"
android:background="@color/colorPrimary"
android:letterSpacing="0.09"
tools:targetApi="lollipop" />
</LinearLayout>
Java代码:
holder.videoView.setVideoURI(Uri.parse(imgPathFile + File.separator + dataModels.get(position).getFileName()));
holder.videoView.setMediaController(new MediaController(context)); //sets MediaController in the video view
holder.videoView.requestFocus();//give focus to a specific view
holder.videoView.start();//starts the video
解决方法
根据我的导师的建议,我使用外部库解决了此问题 来自GitHub的通用视频视图。
如果有人想在他们的项目中添加VideoView,请使用下面的库