特别针对启动页播放视频的问题
1.实现
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv"
android:background="@drawable/my_dog"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
可以看到在视频上面放一个image,当视频准备渲染成功后隐藏image,这个image使用启动图
2.具体解决
val path = "android.resource://" + packageName.toString() + "/" + R.raw.leaf
video.setVideoPath(path)
video.start()
video.setOnPreparedListener(object : MediaPlayer.OnPreparedListener {
override fun onPrepared(mp: MediaPlayer?) {
mp?.setOnInfoListener(object : MediaPlayer.OnInfoListener {
override fun onInfo(mp: MediaPlayer?, what: Int, extra: Int): Boolean {
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
iv.visibility = View.GONE
return true
}
return false
}
})
}
})
这样做的缺点是,让用户感觉启动时间增加了,但比有黑屏要好