5_5-手机开播场景-聊天室
1 Feature Overview
The chatroom module includes features such as speaking, microphone control, and camera control. Compared to other modules, the chatroom module is more complex and extensive in terms of UI, interaction, and functionality. Therefore, designing, building, and maintaining a chatroom is a time-consuming and labor-intensive task. We recommend directly using the chatroom module encapsulated by Polyv. This part of the code is fully open-source, supporting direct use and secondary development.
2 Usage Demo
In the mobile live streaming scenario, the corresponding implementation layout for the chatroom is PLVLSChatroomLayout. The following example demonstrates how to create and initialize the chatroom layout. The code is as follows:
// 聊天室布局
private IPLVLSChatroomLayout plvlsChatroomLy;
// findView
plvlsChatroomLy = findViewById(R.id.plvls_chatroom_ly);
// 初始化聊天室布局
plvlsChatroomLy.init(liveRoomDataManager);
Usage examples of the above methods can be found in the PLVLSLiveStreamerActivity of the polyv demo project.
3 API Introduction
In the mobile live streaming scenario, the interface encapsulated for the chatroom layout defines:
- Methods directly called externally
- Event listeners that require external responses
public interface IPLVLSChatroomLayout {
// <editor-fold defaultstate="collapsed" desc="1、外部直接调用的方法">
/**
* 初始化
*
* @param liveRoomDataManager 直播间数据管理器
*/
void init(IPLVLiveRoomDataManager liveRoomDataManager);
/**
* 设置view交互事件监听器
*
* @param listener 监听器
*/
void setOnViewActionListener(OnViewActionListener listener);
/**
* 添加聊天室在线人数变化监听器
*
* @param listener 监听器
*/
void addOnOnlineCountListener(IPLVOnDataChangedListener<Integer> listener);
/**
* 设置麦克风开关按钮状态
*
* @param isOpen true:打开,false:关闭
*/
void setOpenMicViewStatus(boolean isOpen);
/**
* 设置摄像头开关按钮状态
*
* @param isOpen true:打开,false:关闭
*/
void setOpenCameraViewStatus(boolean isOpen);
/**
* 设置前置摄像头按钮状态
*
* @param isFront true:前置,false:后置
*/
void setFrontCameraViewStatus(boolean isFront);
/**
* 改变聊天室布局可见性
*
* @param visibility 可见性
*/
void setVisibility(int visibility);
/**
* 是否拦截返回事件
*
* @return true:拦截,false:不拦截
*/
boolean onBackPressed();
/**
* 销毁,释放资源
*/
void destroy();
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="2、需要外部响应的事件监听器">
/**
* view交互事件监听器
*/
interface OnViewActionListener {
/**
* 麦克风控制
*
* @param isMute true:禁用,false:启用
*/
boolean onMicControl(boolean isMute);
/**
* 摄像头控制
*
* @param isMute true:禁用,false:启用
*/
boolean onCameraControl(boolean isMute);
/**
* 前置摄像头控制
*
* @param isFront true:前置,false:后置
*/
boolean onFrontCameraControl(boolean isFront);
}
// </editor-fold>
}
4 Implementation Overview
4.1 PLVLSChatroomLayout
This class is the chatroom layout for the mobile live streaming scenario, implementing the IPLVLSChatroomLayout interface.
This layout includes UI components such as the chat message list, message input box, and chatroom toolbar layout.
Below are the main methods involved in this layout.
4.1.1 View Initialization Method
PLVLSChatroomLayout inherits from FrameLayout. In the constructor, the initView method is used to initialize view.
public PLVLSChatroomLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
//填充布局到该view中
LayoutInflater.from(getContext()).inflate(R.layout.plvls_chatroom_layout, this);
//findView
plvlsChatroomChatMsgLy = findViewById(R.id.plvls_chatroom_chat_msg_ly);
plvlsChatroomSwipeLoadView = findViewById(R.id.plvls_chatroom_swipe_load_view);
plvlsChatroomChatMsgRv = findViewById(R.id.plvls_chatroom_chat_msg_rv);
plvlsChatroomUnreadMsgTv = findViewById(R.id.plvls_chatroom_unread_msg_tv);
plvlsChatroomControlIv = findViewById(R.id.plvls_chatroom_control_iv);
plvlsChatroomToolbarMicControlIv = findViewById(R.id.plvls_chatroom_toolbar_mic_control_iv);
plvlsChatroomToolbarCameraControlIv = findViewById(R.id.plvls_chatroom_toolbar_camera_control_iv);
plvlsChatroomToolbarFrontCameraControlIv = findViewById(R.id.plvls_chatroom_toolbar_front_camera_control_iv);
plvlsChatroomToolbarOpenInputWindowTv = findViewById(R.id.plvls_chatroom_toolbar_open_input_window_tv);
//初始化按钮点击事件
plvlsChatroomControlIv.setOnClickListener(this);
plvlsChatroomToolbarMicControlIv.setOnClickListener(this);
plvlsChatroomToolbarCameraControlIv.setOnClickListener(this);
plvlsChatroomToolbarFrontCameraControlIv.setOnClickListener(this);
plvlsChatroomToolbarOpenInputWindowTv.setOnClickListener(this);
//初始化控件...
}
4.1.2 Data Initialization Method
The init method of PLVLSChatroomLayout is an external API that requires external input of IPLVLiveRoomDataManager for initialization.
//聊天室presenter
private IPLVChatroomContract.IChatroomPresenter chatroomPresenter;
@Override
public void init(IPLVLiveRoomDataManager liveRoomDataManager) {
this.liveRoomDataManager = liveRoomDataManager;
//创建聊天室mvp-presenter
this.chatroomPresenter = new PLVChatroomPresenter(liveRoomDataManager);
//注册聊天室mvp-view
this.chatroomPresenter.registerView(chatroomView);
//初始化聊天室mvp-presenter
this.chatroomPresenter.init();
//请求一次历史记录
chatroomPresenter.requestChatHistory(chatroomPresenter.getViewIndex(chatroomView));
//初始化socket登录管理器
initSocketLoginManager();
}
//创建聊天室的mvp-view
private PLVAbsChatroomView chatroomView = new PLVAbsChatroomView() {
//...
}
Here, PLVChatroomPresenter is the chatroom presenter encapsulated using the mvp pattern. It uses presenter to isolate view and mode. All business logic is operated through presenter, meaning presenter serves as the bridge between the view and data, with the view and data separated at both ends.
