如何显示片段恢复后视频的第一帧,而不是视频播放器是空白的?
1。添加surface_type ="texture_view"toPlayerView
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/player_view"
app:surface_type="texture_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
2)。在布局
的PlayerView前面添加缩略图视图<View
android:id="@+id/view_thumbnail_background"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black" />
<ImageView
android:id="@+id/image_thumbnail"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent" />
3)。创建一个方法从TextureView
Bitmap
private fun setVideoThumbnail() {
val textureView = playerView.videoSurfaceView as TextureView
val bitmap = textureView.bitmap
thumbnailImage.setImageBitmap(bitmap)
thumbnailBackground.visibility = View.VISIBLE
thumbnailImage.visibility = View.VISIBLE
}
4)。InonPause
call
setVideoThumbnail()
// destroy player or not depending
5)。为onViewCreated
中的thumbnailImage添加onClick
监听器
thumbnailImage.setOnClickListener {
// make sure player is re-created and media source is loaded, prepared, and seekTo
player.playWhenReady = true
thumbnailBackground.visibility = View.INVISIBLE
thumbnailImage.visibility = View.INVISIBLE
}