android RecyclerView列表自动播放实现
生活随笔
收集整理的這篇文章主要介紹了
android RecyclerView列表自动播放实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近有個需求,類似于皮皮蝦那種列表上有播放器,滑動停止時自動播放可視區域的第一項;
查了一些資料,看了一些大神的博客,受益匪淺,這里也做一個記錄,方便以后搬磚;
要實現這個功能,主要需要四部分工作:
1、監聽列表的滾動事件,知道什么時候開始滾動和停止滾動;
2、找出可視區域的item,再找出第一項中的播放器;
3、監聽移除屏幕的item,及時回收播放器資源;
3、找一個適合的播放器,有些播放器就是不得勁。
直接上代碼,
MainActivity.java
package com.androidlmy.jzplayer;import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.content.Context; import android.graphics.Rect; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import cn.jzvd.Jzvd; import static cn.jzvd.Jzvd.STATE_PAUSE; import static cn.jzvd.Jzvd.STATE_PLAYING;public class MainActivity extends AppCompatActivity {private RecyclerView recyclerView;private PlayerAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);recyclerView = findViewById(R.id.recycler_view);adapter = new PlayerAdapter(this);recyclerView.setLayoutManager(new LinearLayoutManager(this));recyclerView.setAdapter(adapter);recyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {@Overridepublic void onChildViewAttachedToWindow(View view) {// item依附到recyclerview中時}@Overridepublic void onChildViewDetachedFromWindow(View view) {// item從recyclerview中分離時Jzvd jzvd = view.findViewById(R.id.videoplayer);if (jzvd != null && Jzvd.CURRENT_JZVD != null &&jzvd.jzDataSource.containsTheUrl(Jzvd.CURRENT_JZVD.jzDataSource.getCurrentUrl())) {if (Jzvd.CURRENT_JZVD != null && Jzvd.CURRENT_JZVD.screen != Jzvd.SCREEN_FULLSCREEN) {// 釋放播放器資源Jzvd.releaseAllVideos();}}}});recyclerView.addOnScrollListener(new AutoPlayScrollListener(this));}/*** 監聽recycleView滑動狀態,* 自動播放可見區域內的第一個視頻*/private static class AutoPlayScrollListener extends RecyclerView.OnScrollListener {private int firstVisibleItem = 0;private int lastVisibleItem = 0;private int visibleCount = 0;private Context context;public AutoPlayScrollListener(Context context) {this.context = context;}/*** 被處理的視頻狀態標簽*/private enum VideoTagEnum {/*** 自動播放視頻*/TAG_AUTO_PLAY_VIDEO,/*** 暫停視頻*/TAG_PAUSE_VIDEO}@Overridepublic void onScrollStateChanged(RecyclerView recyclerView, int newState) {super.onScrollStateChanged(recyclerView, newState);switch (newState) {case RecyclerView.SCROLL_STATE_IDLE://停止滾動autoPlayVideo(recyclerView, VideoTagEnum.TAG_AUTO_PLAY_VIDEO);case RecyclerView.SCROLL_STATE_DRAGGING://開始滾動case RecyclerView.SCROLL_STATE_SETTLING://滑行中default: // autoPlayVideo(recyclerView, VideoTagEnum.TAG_PAUSE_VIDEO);//滑動時暫停視頻 需要可以加上break;}}@Overridepublic 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)方法計算出哪個播放器完全顯示出來* @param recyclerView* @param handleVideoTag 視頻需要進行狀態*/private void autoPlayVideo(RecyclerView recyclerView, VideoTagEnum handleVideoTag) {for (int i = 0; i < visibleCount; i++) {if (recyclerView != null && recyclerView.getChildAt(i) != null && recyclerView.getChildAt(i).findViewById(R.id.videoplayer) != null) {MyJzvdStd homeGSYVideoPlayer = (MyJzvdStd) recyclerView.getChildAt(i).findViewById(R.id.videoplayer);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.state != STATE_PLAYING)) {// 進行播放homeGSYVideoPlayer.startVideo();}break;case TAG_PAUSE_VIDEO:if ((homeGSYVideoPlayer.state != STATE_PAUSE)) {// 模擬點擊,暫停視頻homeGSYVideoPlayer.startButton.performClick();}break;default:break;}}}/*** 攔截返回鍵 返回不退出程序*/@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (Jzvd.backPress()) {return true;} else {if (keyCode == KeyEvent.KEYCODE_BACK) {moveTaskToBack(true);return true;}}return super.onKeyDown(keyCode, event);} }PlayerAdapter.java
package com.androidlmy.jzplayer;import android.annotation.SuppressLint; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; import cn.jzvd.JzvdStd;public class PlayerAdapter extends RecyclerView.Adapter<PlayerAdapter.ViewHolder> {private Context context;public PlayerAdapter(Context context) {this.context = context;}@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {ViewHolder holder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_recyclerview, parent,false));return holder;}@SuppressLint("LongLogTag")@Overridepublic void onBindViewHolder(ViewHolder holder, int position) { // String url = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4";String url = "https://pointshow.oss-cn-hangzhou.aliyuncs.com/McTk51586843620689.mp4";holder.videoplayer.setUp(url, "", JzvdStd.SCREEN_NORMAL);holder.videoplayer.setVideoImageDisplayType(1);holder.videoplayer.thumbImageView.setScaleType(ImageView.ScaleType.FIT_XY); // holder.videoplayer.startVideo();//自動播放 在recyclerview有bugGlide.with(holder.videoplayer.getContext()).load("http://jzvd-pic.nathen.cn/jzvd-pic/00b026e7-b830-4994-bc87-38f4033806a6.jpg").into(holder.videoplayer.thumbImageView);}@Overridepublic int getItemCount() {return 10;}class ViewHolder extends RecyclerView.ViewHolder {private MyJzvdStd videoplayer;public ViewHolder(View itemView) {super(itemView);videoplayer = itemView.findViewById(R.id.videoplayer);}} }activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recycler_view"android:layout_width="match_parent"android:layout_height="match_parent" /></LinearLayout>item_recyclerview.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="300dp"><com.androidlmy.jzplayer.MyJzvdStdandroid:id="@+id/videoplayer"android:layout_width="match_parent"android:layout_height="match_parent" /></RelativeLayout>build.gradle 中加上播放器和glide的依賴
implementation 'cn.jzvd:jiaozivideoplayer:7.0.5'implementation 'com.github.bumptech.glide:glide:4.8.0'annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'資源下載鏈接
https://download.csdn.net/download/msn465780/87131990
好了,又可以愉快玩耍了。
總結
以上是生活随笔為你收集整理的android RecyclerView列表自动播放实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【OpenGL学习笔记⑧】——键盘控制正
- 下一篇: 怎样用计算机录制声音文件,电脑录音软件推