Giter Site home page Giter Site logo

autovideoplayer's Introduction

本文仿照新浪微博/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' 

autovideoplayer's People

Contributors

zzzlw avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.