Polyv Help Center

Help Center

7_4-核心common-播放器

Updated: 2023-04-17 15:27:57

1 Feature Overview

The player module is located in the polyvLiveCommonModul module under the player package, including live and replay players. This module is designed using the MVP pattern, isolating the view layer from direct calls to the SDK layer's player videoView, and extracting and encapsulating common code for videoView across multiple scenarios, making the view layer logic more concise. The encapsulated presenter layer is responsible for controlling videoView and notifying the view layer to update the UI.

2 MVP Pattern

The MVP pattern for the live player uses IPLVLivePlayerContract as the contract class, which internally defines the live player mvp-view interface ILivePlayerView and the live player mvp-presenter interface ILivePlayerPresenter.

public interface IPLVLivePlayerContract {

    // <editor-fold defaultstate="collapsed" desc="1、mvp-直播播放器view层接口">

    /**
     * mvp-直播播放器view层接口
     */
    interface ILivePlayerView {
        /**
         * 设置presenter后的回调
         */
        void setPresenter(@NonNull ILivePlayerPresenter presenter);

        /**
         * 获取主播放器view
         */
        PolyvLiveVideoView getLiveVideoView();

        /**
         * 获取暖场播放器view
         */
        PolyvAuxiliaryVideoview getSubVideoView();

        /**
         * 获取播放器缓冲视图
         */
        View getBufferingIndicator();

        /**
         * 获取暂无直播显示的视图
         */
        View getNoStreamIndicator();

        /**
         * 子播放器开始播放回调
         *
         * @param isFirst 每次加载完成后是否是第一次start播放
         */
        void onSubVideoViewPlay(boolean isFirst);

        /**
         * 子播放器点击事件
         *
         * @param mainPlayerIsPlaying 主播放器是否在播放中
         */
        void onSubVideoViewClick(boolean mainPlayerIsPlaying);

        /**
         * 主播放器播放失败回调
         *
         * @param error 失败数据
         * @param tips  错误提示
         */
        void onPlayError(PolyvPlayError error, String tips);

        /**
         * 暂无直播回调
         */
        void onNoLiveAtPresent();

        /**
         * 直播推流暂停回调
         */
        void onLiveStop();

        /**
         * 直播结束回调
         */
        void onLiveEnd();

        /**
         * 准备完成回调
         *
         * @param mediaPlayMode 音视频播放模式
         */
        void onPrepared(@PolyvMediaPlayMode.Mode int mediaPlayMode);

        /**
         * 线路切换回调
         *
         * @param linesPos 线路索引
         */
        void onLinesChanged(int linesPos);

        /**
         * 获取跑马灯回调
         *
         * @param marqueeVo  跑马灯数据
         * @param viewerName 观看用户名
         */
        void onGetMarqueeVo(PolyvLiveMarqueeVO marqueeVo, String viewerName);

        /**
         * 重新开始播放回调
         */
        void onRestartPlay();

        /**
         * 手势触发的亮度改变事件
         *
         * @param changeValue 亮度值,范围:[0,100]
         * @param isEnd       手势是否结束
         * @return 是否要改变亮度
         */
        boolean onLightChanged(int changeValue, boolean isEnd);

        /**
         * 手势触发的音量改变事件,changeValue:,return:是否要改变音量
         *
         * @param changeValue 音量值,范围:[0,100]
         * @param isEnd       手势是否结束
         * @return 是否要改变音量
         */
        boolean onVolumeChanged(int changeValue, boolean isEnd);

        /**
         * 该频道直播的服务端弹幕开关
         *
         * @param isServerDanmuOpen true:开启了弹幕,false:关闭了弹幕
         */
        void onServerDanmuOpen(boolean isServerDanmuOpen);

        /**
         * 根据频道的类型,决定是否要显示ppt
         *
         * @param visible {@link View#VISIBLE}
         */
        void onShowPPTView(int visible);

        /**
         * 断网重连
         *
         * @return true表示不使用播放器内部重连逻辑,false表示使用。
         */
        boolean onNetworkRecover();
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="2、mvp-直播播放器presenter层接口">

    /**
     * mvp-直播播放器presenter层接口
     */
    interface ILivePlayerPresenter {
        /**
         * 注册view
         */
        void registerView(@NonNull ILivePlayerView v);

        /**
         * 解除注册的view
         */
        void unregisterView();

        /**
         * 初始化播放器配置
         */
        void init();

        /**
         * 开始播放
         */
        void startPlay();

        /**
         * 重新开始播放
         */
        void restartPlay();

        /**
         * 暂停播放
         */
        void pause();

        /**
         * 恢复播放
         */
        void resume();

        /**
         * 停止播放
         */
        void stop();

        /**
         * 是否在播放中
         */
        boolean isPlaying();

        /**
         * 获取可以切换的线路数量
         */
        int getLinesCount();

        //获取当前线路可以切换的码率(清晰度)信息
        @Nullable
        List<PolyvDefinitionVO> getBitrateVO();

        /**
         * 获取播放模式
         *
         * @return @{@link PolyvMediaPlayMode.Mode}
         */
        int getMediaPlayMode();

        /**
         * 改变播放模式
         */
        void changeMediaPlayMode(@PolyvMediaPlayMode.Mode int mediaPlayMode);

        /**
         * 切换线路
         *
         * @param linesPos 线路索引
         */
        void changeLines(int linesPos);

        /**
         * 切换码率
         *
         * @param bitRate 码率索引
         */
        void changeBitRate(int bitRate);

        /**
         * 截图
         *
         * @return 截图的图片
         */
        @Nullable
        Bitmap screenshot();

        /**
         * 获取视频信息
         *
         * @return 视频信息数据
         */
        @Nullable
        PolyvLiveChannelVO getChannelVO();

        /**
         * 获取当前线路索引
         */
        int getLinesPos();

        /**
         * 获取当前码率(清晰度)索引
         */
        int getBitratePos();

        /**
         * 设置系统音量
         *
         * @param volume 音量值,范围:[0,100]
         */
        void setVolume(int volume);

        /**
         * 获取系统音量
         *
         * @return 音量值,范围:[0,100]
         */
        int getVolume();

        /**
         * 设置播放器音量
         *
         * @param volume 音量值,范围:[0,100]
         */
        void setPlayerVolume(int volume);

        /**
         * 是否需要手势
         *
         * @param need true:需要,false:不需要
         */
        void setNeedGestureDetector(boolean need);

        /**
         * 获取直播播放器数据
         */
        @NonNull
        PLVLivePlayerData getData();

        /**
         * 销毁,包括销毁播放器、解除view
         */
        void destroy();
    }
    // </editor-fold>
}

2.1 Presenter Implementation Logic

The implementation class of ILivePlayerPresenter is PLVLivePlayerPresenter.

It primarily uses the following two classes to complete core business:

  1. IPLVLiveRoomDataManager, the live room data manager
  2. PolyvLiveVideoView, the SDK layer's player videoView
2.1.1 Initialization

PLVLivePlayerPresenter performs the following initialization operations in the constructor: references IPLVLiveRoomDataManager, creates PLVLivePlayerData.

public PLVLivePlayerPresenter(@NonNull IPLVLiveRoomDataManager liveRoomDataManager) {
    this.liveRoomDataManager = liveRoomDataManager;
    livePlayerData = new PLVLivePlayerData();
}

PLVLivePlayerData is the live player data, which internally stores data of type MutableLiveData, mainly used to provide non-mvp views with the ability to listen to/retrieve player data, and can be used for data monitoring and retrieval across different business modules.

2.1.2 Registering mvp-view

After the UI layer creates the mvp-view, by calling the registerView method of the mvp-presenter, the mvp-view can be passed to the mvp-presenter, allowing the mvp-presenter to operate on the mvp-view; at the same time, the mvp-presenter also passes itself to the mvp-view, enabling the mvp-view to call methods provided by the mvp-presenter.

@Override
public void registerView(@NonNull IPLVLivePlayerContract.ILivePlayerView v) {
    this.vWeakReference = new WeakReference<>(v);
    v.setPresenter(this);
}

3 SDK Core Class Introduction

The SDK core class for the live player is PolyvLiveVideoView, which is used in PLVLivePlayerPresenter.

3.1 Creation

PolyvLiveVideoView is a view, so it can be created in the layout.

<com.easefun.polyv.livescenes.video.PolyvLiveVideoView
    android:id="@+id/live_video_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</com.easefun.polyv.livescenes.video.PolyvLiveVideoView>

After finding PolyvLiveVideoView through findViewById, the getLiveVideoView method of the player mvp-view can be used to pass PolyvLiveVideoView to the player mvp-presenter for use.

3.2 Initialization

Some views can be configured for PolyvLiveVideoView based on business needs. Usage example:

PLVLCLiveMediaLayout

private void initVideoView() {
    //设置子播放器view
    videoView.setSubVideoView(subVideoView);
    //设置音频模式view
    videoView.setAudioModeView(audioModeView);
    //设置loadingView
    videoView.setPlayerBufferingIndicator(loadingView);
    //设置无直播时显示的view
    videoView.setNoStreamIndicator(noStreamView);
    //设置直播停止时显示的view
    videoView.setStopStreamIndicator(stopStreamView);
    //设置控制栏
    videoView.setMediaController(mediaController);
    //设置跑马灯
    videoView.post(new Runnable() {
        @Override
        public void run() {
            marqueeView = ((Activity) getContext()).findViewById(R.id.plvlc_marquee_view);//after videoLayout add, post find
            videoView.setMarqueeView(marqueeView, marqueeItem = new PolyvMarqueeItem());
        }
    });
}

3.3 Setting Listeners

PolyvLiveVideoView provides many listeners to monitor player-related information.

/**
 * 设置视频播放完成回调,只有url播放才会触发该事件
 *
 * @param l
 */
public void setOnCompletionListener(IPolyvVideoViewListenerEvent.OnCompletionListener l);

/**
 * 设置视频已准备好马上进入播放回调
 *
 * @param l
 */
public void setOnPreparedListener(IPolyvVideoViewListenerEvent.OnPreparedListener l);

/**
 * 设置视频播放器内部错误回调,只有url播放才会触发该事件
 *
 * @param l
 */
public void setOnErrorListener(IPolyvVideoViewListenerEvent.OnErrorListener l);

/**
 * 设置视频播放器信息有变更回调
 *
 * @param l
 */
public void setOnInfoListener(IPolyvVideoViewListenerEvent.OnInfoListener l);

/**
 * 设置seek完成回调,只有url播放才会触发该事件
 *
 * @param l
 */
public void setOnSeekCompleteListener(IPolyvVideoViewListenerEvent.OnSeekCompleteListener l);

/**
 * 设置视频尺寸改变回调
 *
 * @param l
 */
public void setOnVideoSizeChangedListener(IPolyvVideoViewListenerEvent.OnVideoSizeChangedListener l);


/**
 * 设置视频缓存更新回调,只有url播放才会触发该事件
 *
 * @param l
 */
public void setOnBufferingUpdateListener(IPolyvVideoViewListenerEvent.OnBufferingUpdateListener l);

/**
 * 设置视频播放回调
 *
 * @param l
 */
public void setOnVideoPlayListener(IPolyvVideoViewListenerEvent.OnVideoPlayListener l);

/**
 * 设置视频暂停回调
 *
 * @param l
 */
public void setOnVideoPauseListener(IPolyvVideoViewListenerEvent.OnVideoPauseListener l);


/**
 * 设置暖场图片弹出监听回调
 *
 * @param l
 */
public void setOnCoverImageOutListener(IPolyvVideoViewListenerEvent.OnCoverImageOutListener l);

/**
 * 设置手势左向上回调
 *
 * @param l
 */
public void setOnGestureLeftUpListener(IPolyvVideoViewListenerEvent.OnGestureLeftUpListener l);

/**
 * 设置手势左向下回调
 *
 * @param l
 */
public void setOnGestureLeftDownListener(IPolyvVideoViewListenerEvent.OnGestureLeftDownListener l);

/**
 * 设置手势右向上回调
 *
 * @param l
 */
public void setOnGestureRightUpListener(IPolyvVideoViewListenerEvent.OnGestureRightUpListener l);

/**
 * 设置手势右向下回调
 *
 * @param l
 */
public void setOnGestureRightDownListener(IPolyvVideoViewListenerEvent.OnGestureRightDownListener l);

/**
 * 设置手势左滑回调
 *
 * @param l
 */
public void setOnGestureSwipeLeftListener(IPolyvVideoViewListenerEvent.OnGestureSwipeLeftListener l);

/**
 * 设置手势右滑回调
 *
 * @param l
 */
public void setOnGestureSwipeRightListener(IPolyvVideoViewListenerEvent.OnGestureSwipeRightListener l);

/**
 * 设置手势单击回调
 *
 * @param l
 */
public void setOnGestureClickListener(IPolyvVideoViewListenerEvent.OnGestureClickListener l);

/**
 * 设置手势双击回调
 *
 * @param l
 */
public void setOnGestureDoubleClickListener(IPolyvVideoViewListenerEvent.OnGestureDoubleClickListener l);

/**
 * 设置PPT显示回掉
 *
 * @param l
 */
public void setOnPPTShowListener(IPolyvVideoViewListenerEvent.OnPPTShowListener l);

/**
 * 设置视频重新加载
 *
 * @param l
 */
public void setOnVideoViewRestartListener(IPolyvVideoViewListenerEvent.OnVideoViewRestart l);

/**
 * 设置获取直播后台设置的跑马灯样式的监听器
 *
 * @param l
 */
public void setOnGetMarqueeVoListener(IPolyvVideoViewListenerEvent.OnGetMarqueeVoListener l);

/**
 * 设置SEI信息回调
 *
 * @param l
 */
void setOnSEIRefreshListener(IPolyvVideoViewListenerEvent.OnSEIRefreshListener l);

/**
 * 设置网络状态监听器回调
 *
 * @param l
 */
void setOnNetworkStateListener(IPolyvVideoViewListenerEvent.OnNetworkStateListener l);

Examples of setting listeners can be found in PLVLivePlayerPresenter.

3.4 Start Playback

PolyvLiveVideoView provides a method to start video playback.

/**
 * 点播与直播的播放入口
 *
 * @param params 请求数据实体的结构 通过这个结构可以统一点播与直播的播放入口
 * @param mode   播放的类型,确定需要解析的参数
 */
@MainThread
void playByMode(PolyvBaseVideoParams params, @PLVPlayOption.PlayMode int mode);

This method is encapsulated in PLVLivePlayerPresenter. The player mvp-view calls the startPlay method of PLVLivePlayerPresenter to start video playback.

@Override
public void startPlay() {
    PolyvLiveVideoParams liveVideoParams = new PolyvLiveVideoParams(
            getConfig().getChannelId(),
            getConfig().getAccount().getUserId(),
            getConfig().getUser().getViewerId()
    );
    liveVideoParams.buildOptions(PolyvBaseVideoParams.WAIT_AD, true)
            .buildOptions(PolyvBaseVideoParams.MARQUEE, true)
            .buildOptions(PolyvBaseVideoParams.PARAMS2, getConfig().getUser().getViewerName());
    if (videoView != null) {
        videoView.playByMode(liveVideoParams, PLVPlayOption.PLAYMODE_LIVE);
    }
    startPlayProgressTimer();
}

3.5 Playback Control

PolyvLiveVideoView provides the following playback control methods.

//开始
void start();
//暂停
void pause();
//停止
void stopPlay();

The above methods are also encapsulated in PLVLivePlayerPresenter. For more player-related method calls, refer to the interface ILivePlayerPresenter implemented by PLVLivePlayerPresenter.

联系客服,在线咨询