autovideoplayer

Introduction: 仿照新浪微博列表滑动自动播放暂停视频
More: Author   ReportBugs   DemoAPK   
Tags:
VideoPlayer-ListView-recyclerView-

本文仿照新浪微博/QQ 空间实现了滑动自动播放视频的功能。

本文来自个人博客:http://www.zhangliwei.date

如有疑问欢迎讨论,感谢您的关注。

效果图

滑动自动播放 1 滑动自动播放 2


关键代码

1.监听滚动事件

首先要给 listview 添加 setOnScrollListener 监听,注意这个监听在 recyclerView 上是 addOnScrollListener,也就是说下面代码同时支持 recyclerView。

public int firstVisible=0,visibleCount=0, totalCount=0;
  videoList.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            switch (scrollState) {

                case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                    Log.e("videoTest", "SCROLL_STATE_FLING");
                    break;

                case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                    Log.e("videoTest", "SCROLL_STATE_IDLE");
                    autoPlayVideo(view);
                    break;

                case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                    Log.e("videoTest", "SCROLL_STATE_TOUCH_SCROLL");
                    break;

                default:
                    break;
            }
        }   
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                             int visibleItemCount, int totalItemCount) {
            // firstVisibleItem   当前第一个可见的 item
            // visibleItemCount   当前可见的 item 个数
            if (firstVisible == firstVisibleItem) {
                return;
            }
            firstVisible = firstVisibleItem;
            visibleCount = visibleItemCount;
            totalCount = totalItemCount;
        }
    });

监听里会有两个方法,我们用 onScroll 方法记录 当前第一个可见 Item,以及可见 Item 总数,用 onScrollStateChanged 来监听手滑动屏幕的整个过程。 当 onScrollStateChanged 中的 scrollState 字段值等于 SCROLL_STATE_IDLE 时,代表本次滑动完毕并停止滚动🙄感兴趣的朋友可以自行百度另外两个参数的意思,没准对你的需求有帮助🙄。

2.处理视频逻辑

   void autoPlayVideo(AbsListView view){
    Log.e("videoTest", "firstVisiblePos  =  " + firstVisible + "visibleItemCount =  " + visibleCount);
    for (int i = 0; i < visibleCount; i++) {
        if (view!=null&&view.getChildAt(i)!=null&&view.getChildAt(i).findViewById(R.id.videoplayer) != null) {
            JCVideoPlayerStandard videoPlayerStandard1 = (JCVideoPlayerStandard) view.getChildAt(i).findViewById(R.id.videoplayer);
            Rect rect = new Rect();
            videoPlayerStandard1.getLocalVisibleRect(rect);
            int videoheight3 = videoPlayerStandard1.getHeight();
            Log.e("videoTest","i="+i+"==="+"videoheight3:"+videoheight3+"==="+"rect.top:"+rect.top+"==="+"rect.bottom:"+rect.bottom);
            if (rect.top==0&&rect.bottom==videoheight3)
            {
                if (videoPlayerStandard1.currentState == JCVideoPlayer.CURRENT_STATE_NORMAL || videoPlayerStandard1.currentState == JCVideoPlayer.CURRENT_STATE_ERROR) {
                    Log.e("videoTest", videoPlayerStandard1.currentState + "======================performClick======================");
                    videoPlayerStandard1.startButton.performClick();
                    VPApplication.instance.VideoPlaying=videoPlayerStandard1;
                }
                return;
            }

        }
    }
    Log.e("videoTest", "======================releaseAllVideos=====================");
    JCVideoPlayer.releaseAllVideos();
    VPApplication.instance.VideoPlaying=null;
}

首先是根据总数循环判断 Item 是否有视频,如果有再利用 Rect 类获取视图在屏幕坐标中的可视区域。基本核心代码就这些是不是非常简单?


项目源码 autovideoplay

技术博客 Wells'Note

视频播放器依赖

   compile 'fm.jiecao:jiecaovideoplayer:4.6.3' 
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea