Launching Media in CardView Layout
To get this functionality, you need to solve several problems:
-
You cannot use
VideoView
in the list because it expandsSurfaceView
, and if playback is on and the user starts scrolling, the visual effect will look like the video is trying to catch the scroll. So you need a specialVideoView
one based on something else likeTextureView
. -
Managing the state of the video player : to start / stop playback, we need to call several methods
MediaPlayer.class
:setDataSource() prepare() start() stop() reset() release()
These methods are direct calls to hardware and it may take a while for the hardware to respond. Therefore, we cannot call this on the UI thread. It will block it for more than 16 milliseconds, so the user will see the lagging scroll list. We have to call this from a background thread.
-
You need to track at runtime which view is active on the screen and that view should be rendered.
I created an open source project called VideoPlayerManager .
It is fully documented and some basic streams are handled in the demo application. It's not production ready, but if you need some kind of link on how to get this functionality, you can find it there.
PS As for CardView: this is the usual view. You can put a video player in it and have video playback in CardView.
source to share