tcking / GiraffePlayer2

out of the box android video player(support lazy load, ListView/RecyclerView and hight performance)
Apache License 2.0
377 stars 110 forks source link

how to i stop the videoview while scrolling recyler view ?? is there any solution for that ? #157

Closed prakashmp closed 5 years ago

prakashmp commented 5 years ago

Now Am using like thi.but this way not working for me.could you please correct my code
if (newState == SCROLL_STATE_IDLE) {

                int playPosition = linearLayoutManager.findFirstVisibleItemPosition();
                if (playPosition == -1) {//no visible item
                    return;
                }

                try {
                            int firstCompletelyVisibleItemPosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
                            int lastCompletelyVisibleItemPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();

                            for (int i = firstCompletelyVisibleItemPosition; i <=lastCompletelyVisibleItemPosition; i++) {
                                View viewByPosition = linearLayoutManager.findViewByPosition(i);
                                if (viewByPosition != null) {
                                    VideoView videoView = (VideoView) viewByPosition.findViewById(R.id.video_view);
                                    if (videoView!=null && videoView.isCurrentActivePlayer()) {
                                        videoView.getPlayer().stop();
                                        return;//current active player is visible,do nothing
                                    }
                                }
                            }

                            //try find first visible item (visible part > 50%)
                            if (firstCompletelyVisibleItemPosition >= 0 && playPosition != firstCompletelyVisibleItemPosition) {
                                int[] recyclerView_xy = new int[2];
                                int[] f_xy = new int[2];

                                VideoView videoView = (VideoView) linearLayoutManager.findViewByPosition(playPosition).findViewById(R.id.video_view);
                                videoView.getLocationInWindow(f_xy);
                                recyclerView.getLocationInWindow(recyclerView_xy);
                                int unVisibleY = f_xy[1] - recyclerView_xy[1];

                                if (unVisibleY < 0 && Math.abs(unVisibleY) * 1.0 / videoView.getHeight() > 0.5) {//No visible part > 50%,play next
                                    playPosition = firstCompletelyVisibleItemPosition;
                                }
                            }
                            VideoView videoView = (VideoView) linearLayoutManager.findViewByPosition(playPosition).findViewById(R.id.video_view);
                            if (videoView != null) {
                                videoView.getPlayer().stop();
                            }

                        }
                    }