2

I have a RecyclerView. Each item is a video. This item will fill the entire screen. So only one item will be visible at a time. When I scroll, the next item will be visible. Scrolling will stop at the next item. I am achieving this using PagerSnapHelper. Now, when I set player.playWhenReady = true, the next video (which is not visible in the screen ) is also playing. This should not happen. So I have set player.playWhenReady = false. Now my need is, when I scroll to the next item, the video should play automatically. That is, only the video which is visible in the screen should play. When I scroll, the current video should pause and the next video should play.

Vijai Gunasekar
  • 383
  • 1
  • 3
  • 13

1 Answers1

3

Using Facebook model i.e displaying one video fully one time in screen and halves of before and after the current video. We get the position of an item which is fully visible and set it playing and rest all is set to pause.

Setting a scroll detector to recycler view

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int last = ((LinearLayoutManager) 
 mRecyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
            for (int i = 0; i < list.size(); i++) {
                if (i != last)
                    if(list.get(i).getPlayer()!=null)
                    list.get(i).getPlayer().setPlayWhenReady(false);
                else
                    if(list.get(i).getPlayer()!=null)
                    list.get(i).getPlayer().setPlayWhenReady(true);

            }


        }
    });
Akash Jp
  • 162
  • 1
  • 10
  • 1
    Thank you for the answer. I am not working on it anymore so i am unable to tell if this answer solves the issue or not. So I am not marking it as right answer but have upvoted it. Thank you again. – Vijai Gunasekar Aug 08 '20 at 13:09