vicky kumar
vicky kumar

Reputation: 31

Show videos in recyclerview with exoplayer android

I want to display list of images and videos(fetch from server) in recyclerView with exoPlayer android, not auto play. Only if user click on any video thumbnail then it should to play.

Upvotes: 0

Views: 2757

Answers (1)

Ankit Lathiya
Ankit Lathiya

Reputation: 199

For displaying images and video in recycler view you need to create 2 different view holder in the adapter and use below code to get view type for image and video:-

@Override
    public int getItemViewType(int position) {
        if (data.get(0).isVideo) {
            return Constants.LAYOUT_VIDEO;
        } else {
            return Constants.LAYOUT_IMAGE;
        }
    }

and below is code for onCreateViewHolder:-

@NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == Constants.LAYOUT_VIDEO) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_video, parent, false);
            return new VideoViewHolder(view);
        } else if (viewType == Constants.LAYOUT_IMAGE) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_image, parent, false);
            return new ImageViewHolder(view);
        } 
    }

For more info related to display video in recycler view, you can visit the below-given link:-

https://androidwave.com/exoplayer-in-recyclerview-in-android/

And to disable autoplay just write below line before setting URL to exoplayer:-

player.setPlayWhenReady(true);

Upvotes: 2

Related Questions