实现非常简单,直接步入正题。
实现原理
通过监听RecycleView的OnScrollListener接口。重写onScrollStateChanged && onScrolled 判断视频view在屏幕的位置实现自动播放。
效果图
视频播放使用 JiaoZiVideoPlayer
自定义类MyJzvdStd 继承 JzvdStd
public MyJzvdStd(Context context) {
super(context);
}
public static boolean AUTOPLAY = false;
public MyJzvdStd(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
if (jzDataSource == null || jzDataSource.urlsMap.isEmpty() || jzDataSource.getCurrentUrl() == null) {
Toast.makeText(getContext(), getResources().getString(R.string.nourl), Toast.LENGTH_SHORT).show();
return;
}
if (currentState == CURRENT_STATE_NORMAL) {
startVideo();
onEvent(JZUserAction.ON_CLICK_START_ICON);
AUTOPLAY = true;
} else if (currentState == CURRENT_STATE_PLAYING) {
onEvent(JZUserAction.ON_CLICK_PAUSE);
JZMediaManager.pause();
onStatePause();
AUTOPLAY = false;
} else if (currentState == CURRENT_STATE_PAUSE) {
onEvent(JZUserAction.ON_CLICK_RESUME);
JZMediaManager.start();
onStatePlaying();
AUTOPLAY = true;
} else if (currentState == CURRENT_STATE_AUTO_COMPLETE) {
onEvent(JZUserAction.ON_CLICK_START_AUTO_COMPLETE);
startVideo();
}
break;
default:
super.onClick(v);
break;
}
}
}
该类主要定义是否滑动自动播放的标识,重写onClick方法获取点击播放的事件。
public static boolean AUTOPLAY = false;
点击开始播放:开启滑动自动播放,点击暂停播放停止滑动自动播放
新建FeedScrollListener
public class FeedScrollListener extends RecyclerView.OnScrollListener {
private int firstVisibleItem = 0;
private int lastVisibleItem = 0;
private int visibleCount = 0;
/**
* 视频状态标签
*/
private enum VideoTagEnum {
/**
* 自动播放视频
*/
TAG_AUTO_PLAY_VIDEO,
/**
* 暂停视频
*/
TAG_PAUSE_VIDEO
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
switch (newState) {
case RecyclerView.SCROLL_STATE_IDLE:
if (MyJzvdStd.AUTOPLAY) {
autoPlayVideo(recyclerView, VideoTagEnum.TAG_AUTO_PLAY_VIDEO);
}
break;
default:
autoPlayVideo(recyclerView, VideoTagEnum.TAG_PAUSE_VIDEO);
break;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof LinearLayoutManager) {
LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
firstVisibleItem = linearManager.findFirstVisibleItemPosition();
lastVisibleItem = linearManager.findLastVisibleItemPosition();
visibleCount = lastVisibleItem - firstVisibleItem;
}
}
/**
* 循环遍历 可见区域的播放器
* 然后通过 getLocalVisibleRect(rect)方法计算出哪个播放器完全显示出来
* <p>
* getLocalVisibleRect相关链接:http://www.cnblogs.com/ai-developers/p/4413585.html
*
* @param view
* @param handleVideoTag 视频需要进行状态
*/
private void autoPlayVideo(RecyclerView view, VideoTagEnum handleVideoTag) {
for (int i = 0; i < visibleCount; i++) {
MyJzvdStd homeGSYVideoPlayer = view.getChildAt(i).findViewById(R.id.video);
Rect rect = new Rect();
homeGSYVideoPlayer.getLocalVisibleRect(rect);
int videoheight = homeGSYVideoPlayer.getHeight();
if (rect.top == 0 && rect.bottom == videoheight) {
handleVideo(handleVideoTag, homeGSYVideoPlayer);
break;
}
}
}
/**
* 视频状态处理
*
* @param handleVideoTag 视频需要进行状态
* @param homeGSYVideoPlayer JZVideoPlayer播放器
*/
private void handleVideo(VideoTagEnum handleVideoTag, MyJzvdStd homeGSYVideoPlayer) {
switch (handleVideoTag) {
case TAG_AUTO_PLAY_VIDEO:
if ((homeGSYVideoPlayer.currentState != MyJzvdStd.CURRENT_STATE_PLAYING)) {
// 进行播放
homeGSYVideoPlayer.startVideo();
}
break;
case TAG_PAUSE_VIDEO:
/* if ((homeGSYVideoPlayer.currentState != MyJzvdStd.CURRENT_STATE_PAUSE)) {
// 模拟点击播放Button,实现暂停视频
homeGSYVideoPlayer.startButton.performClick();
}*/
break;
default:
break;
}
}
}
滑动播放的主要逻辑
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
switch (newState) {
case RecyclerView.SCROLL_STATE_IDLE:
if (MyJzvdStd.AUTOPLAY) {
autoPlayVideo(recyclerView, VideoTagEnum.TAG_AUTO_PLAY_VIDEO);
}
break;
default:
autoPlayVideo(recyclerView, VideoTagEnum.TAG_PAUSE_VIDEO);
break;
}
}
后记
先讲到这里吧,喜欢的小伙伴别忘了关注和点赞哦~
附上完整项目地址:https://github.com/dingshuangdian/hodgepodge
欢迎留言交流