Android implement auto play video inside the nested RecyclerView like Instagram auto play video

Viewed 472

I would like to implement an Instagram kind of auto-play video player feature inside my app and for the same, I did try a few libraries and also tried a few ways to set up callbacks to pass lifecycle-related callback from fragment to Vertical RecyclerView to Inside Vertical RecyclerView.

I went through the following libraries:

  • arthur3486/ARVI: This library looks more promising and it's using ExoPlayer (i.e. Google developed media player for Android) under the hood, this library is providing more customization and providing more memory management features. It provides releases of Media player objects and also provides auto-play modes.

  • MostafaAnter/VideoPlayerInsideRecyclerView: This library also using ExoPlayer as a media player but using hardware acceleration to use texture view for video player UI designing. This is also managed by a single developer but it was recently updated.

  • google/ExoPlayer: This is Google developed Android media player. This is a vanilla media player library that we can directly use but we need to implement all the management of when and which post media shall app plays and how app shall release those media.

  • mobileappsvn/AutoPlayVideoRecyclerView: This library code is fully written in Java and it has been the last update 2-4 years before. As per their readme, This library is targetting some pinpoints of these features. But still, this library is not providing any information related to two levels of RecyclerView video player management. So we shall have to give it a try and make sure it should work or not.

  • klinker24/Android-SimpleVideoView: This is also a good library and updates 2 years back. It's fully developed in Java and managed by a single developer so far. But again, We need to test with 2 levels of management using this library as well. I found some related gist examples as well so I can give it a try and make sure it should meet our requirements.

I found that arthur3486/ARVI seems promising and I integrated it and tried to set up all the stuff as per the provided guidelines but it is not working and getting unsolvable errors.

1 Answers

Here is a cool library to handle lifecycle events as delegated fields, with a bit of imagination you can avoid all boilerplate from my next example https://github.com/Link184/Lifecycle-Delegates

Or, thanks to google arch components we can do something like this:

class MyAdapter(): ... {
    

    class ViewHolder(): ..., LifecycleObserver {
        init { 
            itemView.findViewTreeLifecycleOwner()?.lifecycle?.addObserver(this)
        }

        @OnLifecycleEvent(ON_PAUSE)
        fun pause() { player.pause() }

        @OnLifecycleEvent(ON_RESUME)
        fun resume() { player.resume() }

        @OnLifecycleEvent(ON_DESTROY)
        fun destroy() { player.release() }
    }

}
Related