Launching Media in CardView Layout

Is there a way to create a media file ( audio/video

) in a layout cardview

? I want to have a preview of the video

file inside card layout

when the user clicks on the map, and video/audio

is captured in full screen and played just like in a Facebook feed.

+1


source to share


1 answer


To get this functionality, you need to solve several problems:

  • You cannot use VideoView

    in the list
    because it expands SurfaceView

    , 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 special VideoView

    one based on something else like TextureView

    .

  • 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.

+12


source







All Articles