1. 程式人生 > >Android中使用JiaoZiVideoPlayer來實現視訊列表播放的效果

Android中使用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:如果相應其他效果的,去多看看原始碼吧。