本文共 6971 字,大约阅读时间需要 23 分钟。
rvList.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); firstVisibleItem = layoutManager.findFirstVisibleItemPosition(); lastVisibleItem = layoutManager.findLastVisibleItemPosition(); } @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { switch (newState) { case RecyclerView.SCROLL_STATE_IDLE://停止滚动 /**在这里执行,视频的自动播放与停止*/ autoPlayVideo(recyclerView); break; case RecyclerView.SCROLL_STATE_DRAGGING://拖动 autoPlayVideo(recyclerView); break; case RecyclerView.SCROLL_STATE_SETTLING://惯性滑动 JZVideoPlayer.releaseAllVideos(); break; } } });
urlList = new ArrayList<>(); urlList.add("http://image.38.hn/public/attachment/201805/100651/201805181532123423.mp4"); urlList.add("http://image.38.hn/public/attachment/201803/100651/201803151735198462.mp4"); urlList.add("http://image.38.hn/public/attachment/201803/100651/201803150923220770.mp4"); urlList.add("http://image.38.hn/public/attachment/201803/100651/201803150922255785.mp4"); urlList.add("http://image.38.hn/public/attachment/201803/100651/201803150920130302.mp4"); urlList.add("http://image.38.hn/public/attachment/201803/100651/201803141625005241.mp4"); urlList.add("http://image.38.hn/public/attachment/201803/100651/201803141624378522.mp4"); urlList.add("http://image.38.hn/public/attachment/201803/100651/201803131546119319.mp4"); videoAdapter = new ListVideoAdapter(urlList); rvList.setLayoutManager(new LinearLayoutManager(ListActivity.this)); rvList.setAdapter(videoAdapter);
class ListVideoAdapter extends BaseRecAdapter{ public ListVideoAdapter(List list) { super(list); } @Override public void onHolder(VideoViewHolder holder, String bean, int position) { holder.mp_video.setUp(bean, JZVideoPlayerStandard.CURRENT_STATE_NORMAL); if (position == 0) { holder.mp_video.startVideo(); } Glide.with(context).load(bean).into(holder.mp_video.thumbImageView); holder.tv_title.setText("第" + position + "个视频"); } @Override public VideoViewHolder onCreateHolder() { return new VideoViewHolder(getViewByRes(R.layout.item_video)); } } public class VideoViewHolder extends BaseRecViewHolder { public View rootView; public MyVideoPlayer mp_video; public TextView tv_title; public VideoViewHolder(View rootView) { super(rootView); this.rootView = rootView; this.mp_video = rootView.findViewById(R.id.mp_video); this.tv_title = rootView.findViewById(R.id.tv_title); } }
/** * 自动播放 */ private void autoPlayVideo(RecyclerView recyclerView) { if (firstVisibleItem == 0 && lastVisibleItem == 0 && recyclerView.getChildAt(0) != null) { MyVideoPlayer videoView = null; if (recyclerView != null && recyclerView.getChildAt(0) != null) { videoView = recyclerView.getChildAt(0).findViewById(R.id.mp_video); } if (videoView != null) { if (videoView.currentState == JZVideoPlayer.CURRENT_STATE_NORMAL || videoView.currentState == JZVideoPlayer.CURRENT_STATE_PAUSE) { videoView.startVideo(); } } } for (int i = 0; i <= lastVisibleItem; i++) { if (recyclerView == null || recyclerView.getChildAt(i) == null) { return; } MyVideoPlayer videoView = recyclerView.getChildAt(i).findViewById(R.id.mp_video); if (videoView != null) { Rect rect = new Rect(); //获取视图本身的可见坐标,把值传入到rect对象中 videoView.getLocalVisibleRect(rect); //获取视频的高度 int videoHeight = videoView.getHeight(); if (rect.top <= 100 && rect.bottom >= videoHeight) { if (videoView.currentState == JZVideoPlayer.CURRENT_STATE_NORMAL || videoView.currentState == JZVideoPlayer.CURRENT_STATE_PAUSE) { videoView.startVideo(); } return; } JZVideoPlayer.releaseAllVideos(); } else { JZVideoPlayer.releaseAllVideos(); } } }
@Override public void onAutoCompletion() { thumbImageView.setVisibility(View.GONE); if (currentScreen == SCREEN_WINDOW_FULLSCREEN) { onStateAutoComplete(); setUp((String) getCurrentUrl(), JZVideoPlayer.SCREEN_WINDOW_FULLSCREEN); } else { super.onAutoCompletion(); setUp((String) getCurrentUrl(), JZVideoPlayer.CURRENT_STATE_NORMAL); } //循环播放 startVideo(); }
转载地址:http://twaal.baihongyu.com/