Android视频列表自动播放功能

效果

直接上代码

        public int firstVisible = 0, visibleCount = 1, totalCount = 0;

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);


    mVideoList.setAdapter(new VideoListAdapter(getActivity()));
    mVideoList.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) {

            if (firstVisible == firstVisibleItem) {
                return;
            }
            firstVisible = firstVisibleItem;
            visibleCount = visibleItemCount;
            totalCount = totalItemCount;
        }
    });

}

@Override
//fragment 左右滑动切换或切后台重新判断
public void setUserVisibleHint(boolean isVisibleToUser) {

    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        visibleCount = 1;
        // 相当于onResume()方法--获取焦点

    } else {
        visibleCount = 0;
        // 相当于onpause()方法---失去焦点

    }
    autoPlayVideo(mVideoList);
}


void autoPlayVideo(AbsListView view) {
//遍历当前界面显示的每个播放器
    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();
            //当前播放器能完全显示
            if (rect.top == 0 && rect.bottom == videoheight3) {
                if (videoPlayerStandard1.currentState == JCVideoPlayer.CURRENT_STATE_NORMAL || videoPlayerStandard1.currentState == JCVideoPlayer.CURRENT_STATE_ERROR) {
                //调用开始播放的按钮
                    videoPlayerStandard1.startButton.performClick();

                }
                return;
            }

        }
    }
    JCVideoPlayer.releaseAllVideos();

}

———公司项目就不放上来了

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