Polyv Help Center

Help Center

6_7-互动学堂场景-连麦

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

1 Feature Overview

The interactive classroom supports a co-hosting feature, allowing students to automatically connect when entering the classroom, raise their hands to request co-hosting, and also allowing the instructor to directly invite students to co-host. It supports multi-user co-hosting to enrich classroom content.

Student hand-raising co-hosting process: Student raises hand to request co-hosting -> Instructor approves the student's co-hosting request -> Student officially joins the co-host.

Instructor invitation co-hosting process: Instructor selects a student to invite to co-host -> Student clicks confirm to join immediately, or automatically joins after 5 seconds.

2 Usage Demo

The co-hosting feature of the interactive classroom is introduced as a layout, with corresponding parameters set during initialization and callbacks monitored.

plvhcLinkmicLy = findViewById(R.id.plvhc_linkmic_ly);
// 初始化连麦布局
plvhcLinkmicLy.init(liveRoomDataManager);
// 初始化连麦的媒体配置
boolean isOpenMic = getIntent().getBooleanExtra(EXTRA_IS_OPEN_MIC, true);
boolean isOpenCamera = getIntent().getBooleanExtra(EXTRA_IS_OPEN_CAMERA, true);
boolean isFrontCamera = getIntent().getBooleanExtra(EXTRA_IS_FRONT_CAMERA, true);
plvhcLinkmicLy.muteAudio(!isOpenMic);
plvhcLinkmicLy.muteVideo(!isOpenCamera);
plvhcLinkmicLy.switchCamera(isFrontCamera);
// 设置布局回调 - 连麦
plvhcLinkmicLy.setOnViewActionListener(new IPLVHCLinkMicLayout.OnViewActionListener() {
    // 连麦布局回调方法 ...
});

For detailed calling code and communication with other modules, please refer to the code calls in the PLVHCLiveHiClassActivity class.

3 Interface Introduction

The interface for the interactive classroom co-hosting layout is defined as IPLVHCLinkMicLayout. The interface defines functional methods that can be directly called externally, as well as event listeners that require external responses.

  1. Functional methods provided for external direct calls
/**
 * 初始化
 *
 * @param liveRoomDataManager 直播间数据管理器
 */
void init(IPLVLiveRoomDataManager liveRoomDataManager);

/**
 * 设置view交互事件监听器
 *
 * @param listener 监听器
 */
void setOnViewActionListener(OnViewActionListener listener);

/**
 * 静音音频
 *
 * @param mute true表示静音,false表示打开
 */
void muteAudio(boolean mute);

/**
 * 禁用视频
 *
 * @param mute true表示禁用视频,false表示打开视频
 */
void muteVideo(boolean mute);

/**
 * 切换前后置摄像头方向
 */
void switchCamera(boolean front);

/**
 * 上课
 */
void startLesson(IPLVDataRequestListener<String> listener);

/**
 * 下课
 */
void stopLesson(IPLVDataRequestListener<String> listener);

/**
 * 发送举手事件
 *
 * @param raiseHandTime 举手时间
 */
void sendRaiseHandEvent(int raiseHandTime);

/**
 * 设置布局的可见性
 *
 * @param visibility One of {@link android.view.View#VISIBLE}, {@link android.view.View#INVISIBLE}, or {@link android.view.View#GONE}.
 */
void setVisibility(int visibility);

/**
 * 是否是课节上课状态
 *
 * @return true:是课节上课状态,false:不是课节上课状态
 */
boolean isLessonStarted();

/**
 * 获取连麦Presenter
 *
 * @return linkMicPresenter
 */
IPLVMultiRoleLinkMicContract.IMultiRoleLinkMicPresenter getLinkMicPresenter();

/**
 * 销毁,释放资源
 */
void destroy();
  1. Event listeners requiring external responses
/**
 * view交互事件监听器
 */
interface OnViewActionListener {
    /**
     * 布局大小变化
     */
    void onLayoutSizeChanged();

    /**
     * 用户举手变化
     *
     * @param raiseHandCount 举手数量
     * @param isRaiseHand    是否举手
     */
    void onUserRaiseHand(int raiseHandCount, boolean isRaiseHand);

    /**
     * 我的画笔权限变化
     *
     * @param isHasPaint 是否有画笔权限
     */
    void onHasPaintToMe(boolean isHasPaint);

    /**
     * 设置连麦readerView
     */
    void onSetupLinkMicRenderView(SurfaceView surfaceView, String linkMicId);

    /**
     * 学生获取奖杯
     *
     * @param userName 用户名称
     */
    void onGetCup(String userName);

    /**
     * 网络质量回调
     *
     * @param networkQuality {@link com.plv.linkmic.PLVLinkMicConstant.NetQuality}
     */
    void onNetworkQuality(int networkQuality);

    /**
     * 上行流量网络状态
     *
     * @param networkStatusVO
     */
    void onUpstreamNetworkStatus(PLVNetworkStatusVO networkStatusVO);

    /**
     * 远端连麦用户网络状态
     *
     * @param networkStatusVO
     */
    void onRemoteNetworkStatus(PLVNetworkStatusVO networkStatusVO);

    /**
     * 课节准备中
     */
    void onLessonPreparing(long serverTime, long lessonStartTime);

    /**
     * 课节开始
     */
    void onLessonStarted();

    /**
     * 课节结束
     */
    void onLessonEnd(long inClassTime, boolean isTeacherType, boolean hasNextClass);
}

4 Implementation Introduction

The implementation class of IPLVHCLinkMicLayout is PLVHCLinkMicLayout, and the same layout is shared by both instructors and students.

4.1 Initializing the View

Initializing the view is completed in two stages: the first stage is layout container initialization, and the second stage is layout content initialization.

4.1.1 Layout Container Initialization

Layout container initialization begins when PLVHCLinkMicLayout is constructed, including adding the co-hosting view stub, starting the foreground service, and keeping the screen on.

private void initView() {
    LayoutInflater.from(getContext()).inflate(R.layout.plvhc_linkmic_layout, this, true);

    classStopNoNextDialog = new PLVHCClassStopNoNextDialog(getContext());
    classStopHasNextDialog = new PLVHCClassStopHasNextDialog(getContext());

    // 启动前台服务,防止在后台被杀
    PLVForegroundService.startForegroundService(PLVHCLiveHiClassActivity.class, "POLYV互动学堂", R.drawable.plvhc_ic_launcher);
    // 防止自动息屏、锁屏
    setKeepScreenOn(true);
}
4.1.2 Layout Content Initialization

Layout content initialization is executed after obtaining the channel's limit on the number of co-hosts.

The interactive classroom supports 1v6 and 1v16 co-hosting modes. The arrangement of co-hosting views differs between these two modes, so the specific layout content is initialized only after determining the number of co-hosts in the channel.

Layout content initialization includes creating the co-host list view, adjusting view dimensions, setting up co-host view callback listeners, and executing delayed tasks that need to run after initialization.

private void initLinkMicItemLayout(boolean isLittleLayout) {
    if (isLittleLayout) {
        ViewStub linkMicLyStub = findViewById(R.id.plvhc_linkmic_little_ly_stub);
        linkMicItemLayout = (IPLVHCLinkMicItemLayout) linkMicLyStub.inflate();
        initLayoutSize(true);
    } else {
        ViewStub linkMicLyStub = findViewById(R.id.plvhc_linkmic_large_ly_stub);
        linkMicItemLayout = (IPLVHCLinkMicItemLayout) linkMicLyStub.inflate();
        initLayoutSize(false);
    }
    if (linkMicItemLayout != null) {
        linkMicItemLayout.setOnViewActionListener(new IPLVHCLinkMicItemLayout.OnViewActionListener() {
            @Override
            public void onClickItemView(final int position, final PLVLinkMicItemDataBean linkMicItemDataBean) {
                // 连麦视图点击回调 ...
            }
        });
        linkMicItemLayout.setOnRenderViewCallback(new PLVHCLinkMicItemView.OnRenderViewCallback() {
            // 连麦渲染视图的回调监听 ...
        });
        // 延时任务执行
        for (Runnable task : initiatedTasks) {
            task.run();
        }
        initiatedTasks.clear();
    }
}

4.2 Initializing Data

Data initialization includes receiving and saving the data manager IPLVLiveRoomDataManager, monitoring lesson details data, and registering and initializing the Presenter IMultiRoleLinkMicPresenter.

@Override
public void init(IPLVLiveRoomDataManager liveRoomDataManager) {
    this.liveRoomDataManager = liveRoomDataManager;
    observeLessonDataBean();

    linkMicPresenter = new PLVMultiRoleLinkMicPresenter(liveRoomDataManager);
    linkMicPresenter.registerView(linkMicView);
    linkMicPresenter.init();
}

Co-hosting is implemented using the MVP pattern, with specific interaction logic implemented in linkMicView. For details, refer to the code block Co-hosting - MVP Pattern View Layer Implementation.

5 Subdirectory Introduction

5.1 item

The item directory stores the co-host list layout view and co-host views.

IPLVHCLinkMicItemLayout defines the interface provided externally by the co-host list layout view.

PLVHCAbsLinkMicItemLayout is the parent class of the co-host list layout view, providing common method implementations for various co-host list layouts.

PLVHCLinkMicItemLittleLayout is the layout view for the 1v6 co-hosting mode, and PLVHCLinkMicItemLargeLayout is the layout view for the 1v16 co-hosting mode.

PLVHCLinkMicItemView is the rendering view for a single co-host.

5.2 widget

The widget directory stores view controls specific to the co-hosting feature, including the countdown popup for students invited to co-host.

联系客服,在线咨询