Android中使用JiaoZiVideoPlayer来实现视频列表播放的效果

目的:我这边是想做类似于斗鱼直播里的视频模块的视频列表播放形式。

思路:使用第三方插件--JiaoZiVideoPlayer。GitHub地址:https://github.com/lipangit/JiaoZiVideoPlayer

然后下载代码,根据自己需要的样式去找相应的代码进行研究。

效果图如下:

使用步骤:

(1)添加远程依赖:

/*调用Video视频播放器*/
implementation 'cn.jzvd:jiaozivideoplayer:6.3.1'

(2)在布局文件中使用:

<cn.jzvd.JzvdStd
    android:id="@+id/Layout_Item_Video_JzVdStd"
    android:layout_width="match_parent"
    android:layout_height="200dp"></cn.jzvd.JzvdStd>

(3)在视频列表RecyclerView的数据适配器中调用(只贴相关代码):

holder.jzvdStd.setUp(tb_videoPlay.getVideoUrl()
        , "简直不可思议啊【" + position + "】"
        , Jzvd.SCREEN_WINDOW_NORMAL);
Glide.with(mContext).load(tb_videoPlay.getThumb()).asBitmap().placeholder(R.mipmap.bg_live)
        .error(R.mipmap.bg_live).into(holder.jzvdStd.thumbImageView);

(4)在清单文件中配置相应的Activity:

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="adjustPan"
    android:configChanges="screenSize|keyboardHidden|orientation">

(5)如果想要某一个item离开屏幕的时候,该item的视频播放停止的效果。那么RecyclerView需要对item离开的状态进行监听,添加监听OnChildAttachStateChangeListener,代码如下:

rViewAnchorVideoList.setAdapter(videoAdapter);
rViewAnchorVideoList.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
    @Override
    public void onChildViewAttachedToWindow(View view) {

    }

    @Override
    public void onChildViewDetachedFromWindow(View view) {
        Jzvd jzvd = view.findViewById(R.id.Layout_Item_Video_JzVdStd);
        if (jzvd != null && jzvd.jzDataSource.containsTheUrl(JZMediaManager.getCurrentUrl())) {
            Jzvd currentJzvd = JzvdMgr.getCurrentJzvd();
            if (currentJzvd != null && currentJzvd.currentScreen != Jzvd.SCREEN_WINDOW_FULLSCREEN) {
                Jzvd.releaseAllVideos();
            }
        }
    }
});

(6)最重要的一点,别忘了添加用户权限:

<uses-permission android:name="android.permission.INTERNET"/>

PS:如果相应其他效果的,去多看看源码吧。

猜你喜欢

转载自blog.csdn.net/lpCrazyBoy/article/details/83383330