列表视频滑动之自动播放

可应用于饺子播放器过着GSY播放器
原理都相同

public class AutoPlayScrollListener extends RecyclerView.OnScrollListener {

    private int firstVisibleItem = 0;
    private int lastVisibleItem = 0;
    private int visibleCount = 0;

    /**
     * 被处理的视频状态标签
     */
    private enum VideoTagEnum {

        /**
         * 自动播放视频
         */
        TAG_AUTO_PLAY_VIDEO,

        /**
         * 暂停视频
         */
        TAG_PAUSE_VIDEO
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);

        switch (newState) {
            case RecyclerView.SCROLL_STATE_IDLE:
                autoPlayVideo(recyclerView, VideoTagEnum.TAG_AUTO_PLAY_VIDEO);
            default:
                // 滑动时暂停视频 autoPlayVideo(recyclerView, VideoTagEnum.TAG_PAUSE_VIDEO);
                break;
        }

    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof LinearLayoutManager) {
            LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
            firstVisibleItem = linearManager.findFirstVisibleItemPosition();
            lastVisibleItem = linearManager.findLastVisibleItemPosition();
            visibleCount = lastVisibleItem - firstVisibleItem;
        }

    }

    /**
     * 循环遍历 可见区域的播放器
     * 然后通过 getLocalVisibleRect(rect)方法计算出哪个播放器完全显示出来
     * 

* getLocalVisibleRect相关链接:http://www.cnblogs.com/ai-developers/p/4413585.html * * @param recyclerView * @param handleVideoTag 视频需要进行状态 */ private void autoPlayVideo(RecyclerView recyclerView, VideoTagEnum handleVideoTag) { for (int i = 0; i < visibleCount; i++) { if (recyclerView != null && recyclerView.getChildAt(i) != null && recyclerView.getChildAt(i).findViewById(R.id.videoplayer) != null) { Jzvd homeGSYVideoPlayer = (Jzvd) recyclerView.getChildAt(i).findViewById(R.id.videoplayer); Rect rect = new Rect(); homeGSYVideoPlayer.getLocalVisibleRect(rect); int videoheight = homeGSYVideoPlayer.getHeight(); if (rect.top == 0 && rect.bottom == videoheight) { handleVideo(handleVideoTag, homeGSYVideoPlayer); // 跳出循环,只处理可见区域内的第一个播放器 break; } } } } /** * 视频状态处理 * * @param handleVideoTag 视频需要进行状态 * @param homeGSYVideoPlayer JZVideoPlayer播放器 */ private void handleVideo(VideoTagEnum handleVideoTag, Jzvd homeGSYVideoPlayer) { switch (handleVideoTag) { case TAG_AUTO_PLAY_VIDEO: // 像下面逻辑判断的话,显示的现象是:停止滑动后不会自动播放那些 ”已经播放完一遍的视频了“ // homeGSYVideoPlayer.currentState== JZVideoPlayerStandard.CURRENT_STATE_NORMAL if ((homeGSYVideoPlayer.state == Jzvd.STATE_NORMAL)) { // 进行播放 homeGSYVideoPlayer.startVideo(); } break; case TAG_PAUSE_VIDEO: if ((homeGSYVideoPlayer.state != Jzvd.STATE_PAUSE)) { // 模拟点击,暂停视频 homeGSYVideoPlayer.startButton.performClick(); } break; default: break; } } }

你可能感兴趣的:(列表视频滑动之自动播放)