1. 程式人生 > >Android視訊列表自動播放功能

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();

}

———公司專案就不放上來了