环信直播聊天室(以下简称直播聊天室)展示了环信 IM SDK直播聊天室的能力。除了提供基本的聊天外,还提供了赠送礼物的自定义消息,开发者可以根据自己的实际需求添加新的自定义消息。
核心类介绍:
- CdnLiveAudienceActivity:观众直播间页面
- CdnLiveHostActivity:主播直播页面
- LiveAudienceFragment:集成观众端聊天室相关逻辑
- LiveAnchorFragment:集成主播端聊天室相关逻辑
其他端开源地址:
- iOS: https://github.com/AgoraIO-Usecase/AgoraChat-Livestream/tree/main/livestream-ios
- Web: https://github.com/AgoraIO-Usecase/AgoraChat-Livestream/tree/main/livestream-web
- App Server: https://github.com/AgoraIO-Usecase/AgoraChat-Livestream/tree/main/backend
- Android Studio 3.6或更高版本。
- SDK targetVersion至少为26。
implementation 'io.hyphenate:hyphenate-chat:3.9.9'
implementation 'io.hyphenate:ease-im-kit:3.9.9'
可通过一下地址查看最新SDK版本号:
集成文档:
为方便开发者使用,项目中将聊天室相关逻辑放到了chatroom
library 中,开发者可以根据自己的需求对这个库进行更改。
- EaseChatRoomMessagesView:直播聊天室消息UI,用户可以自定义相关属性设置UI显示。
- EaseLiveMessageHelper:用于监听接收自定义消息,发送自定义消息。
- EaseLiveMessageType:用户定义了demo中用到的自定义消息类型(礼物消息)。
- OnLiveMessageListener:监听聊天室消息接受相关事件。
EaseLiveMessageHelper.getInstance().init(chatroomId);
EaseLiveMessageHelper.getInstance().addLiveMessageListener(new OnLiveMessageListener() {
@Override
public void onGiftMessageReceived(EMMessage message) {
}
});
EaseLiveMessageHelper.getInstance().removeLiveMessageListener(this);
public void sendTxtMsg(String content, OnSendLiveMessageCallBack callBack); //文本消息
public void sendGiftMsg(String chatRoomId, String giftId, int num, OnSendLiveMessageCallBack callBack); //礼物消息
public void sendCustomMsg(String chatRoomId, String event, Map<String, String> params, final OnSendLiveMessageCallBack callBack); //自定义消息
(1)如果发送的自定义参数与library中相同,可以直接调用如下方法,获得所传的数据
//获取礼物消息中礼物的id
public String getMsgGiftId(EMMessage msg);
//获取礼物消息中礼物的数量
public int getMsgGiftNum(EMMessage msg);
(2)如果自定义消息参数与library中不同,可以调用如下方法,获取消息中的参数
public Map<String, String> getCustomMsgParams(EMMessage message);
public boolean isGiftMsg(EMMessage msg); //礼物消息判断
环信聊天室中有两个repository,ClientRepository及AppServerRepository。其中ClientRepository用户处理环信 SDK提供
的相关请求,AppServerRepository用户处理app server提供的接口。每个页面有相应的ViewModel以生命周期的方式存储和管
理与UI相关的数据。LiveData是一个具有生命周期感知特性的可观察的数据保持类,一般位于ViewModel中,用于观察数据变化。
环信聊天室提供了Agora CDN直播能力(https://docs.agora.io/cn/live-streaming/landing-page?platform=Android)。
按照以下步骤运行示例项目:project:\
git clone git@github.com:easemob/livestream_demo_android.git
在local.properties中配置 CHAT_APPKEY
和 AGORA_APP_ID
,你可以在开发者控制台申请相应的APPKEY。
CHAT_APPKEY=*******
AGORA_APP_ID=*******