房间管理
更新时间: 2024/11/26 15:44:05
本文介绍在 K 歌场景中用户加入房间和离开 KTV 房间的操作。
注意事项
KTV 房间的保留时长最多为 24 小时,超过 24 小时,NERoom Server会自动删除该房间。
功能原理
房间管理的时序流程图如下图所示。
实现流程
-
房主创建 KTV 房间。
业务方可直接调用 NERoom SDK 的接口创建房间,也可以通过业务方的 Server 调用 NERoom Server 去创建房间。创建 KTV 房间的具体实现方式请参见创建房间。
-
调用
joinRoom
接口加入 KTV 房间。 -
调用
getRemoteMembers
接口查询房间成员列表。 -
调用
setLocalAudioProfile
接口,设置音频profile
类型为HighQualityStereo
,设置scenario
为MUSIC
。 -
调用
leaveRoom
接口离开 KTV 房间。 -
调用
endRoom
接口结束 KTV 房间。
示例代码
//创建房间
NERoomService roomService = NERoomKit.getInstance().getService(NERoomService.class);
NECreateRoomParams params = new NECreateRoomParams(
"roomUuid",
"roomName",
400,
null,
new NESeatInitParams(8, NESeatRequestApprovalMode.ON),
Collections.emptyMap()
);
NECreateRoomOptions options = new NECreateRoomOptions(
false, false, true, true, false
);
roomService.createRoom(params, options, new NECallback2<Unit>() {
@Override
public void onSuccess(@Nullable Unit data) {
// 创建KTV房间成功
}
@Override
public void onError(int code, @Nullable String message) {
// todo:创建KTV房间失败
}
});
//加入房间
private var currentRoomContext: NERoomContext? = null
NERoomKit.getInstance().getService(NERoomService::class.java).joinRoom(
NEJoinRoomParams(roomUuid = roomUuid, userName = userName, role = role),
NEJoinRoomOptions(),
object : NECallback2<NERoomContext>() {
override fun onSuccess(data: NERoomContext?) {
currentRoomContext = data!!
setAudioProfile()
addRoomListener()
addNERoomRtcStatsListener()
addSeatListener()
currentRoomContext?.rtcController?.setClientRole(NERoomRtcClientRole.AUDIENCE)
joinRtcChannel(object : NECallback2<Unit>() {
override fun onSuccess(data: Unit?) {
joinChatroomChannel(object : NECallback2<Unit>() {
override fun onSuccess(data: Unit?) {
callback.onSuccess(data)
}
override fun onError(code: Int, message: String?) {
)
leaveRtcChannel(object : NECallback2<Unit>() {
override fun onSuccess(data: Unit?) {
}
override fun onError(code: Int, message: String?) {
}
})
callback.onError(code, message)
}
})
}
override fun onError(code: Int, message: String?) {
callback.onError(code, message)
}
})
}
}
)
//查询房间成员列表
private final NERoomService roomService = NERoomKit.getInstance().getService(NERoomService.class);
roomService.getRoomContext(roomUuid).getRemoteMembers()
// 设置音频属性
private fun setAudioProfile() {
currentRoomContext?.rtcController?.setLocalAudioProfile(
NERoomRtcAudioProfile.HIGH_QUALITY_STEREO,
NERoomRtcAudioScenario.MUSIC
)
currentRoomContext?.rtcController?.setChannelProfile(NERoomRtcChannelProfile.liveBroadcasting)
}
// 离开房间
fun leaveRoom(callback: NECallback<Unit>) {
audioPlayService?.destroy()
currentRoomContext?.leaveRoom(callback)
}
//结束房间
fun endRoom(callback: NECallback<Unit>) {
currentRoomContext?.endRoom(callback)
}
此文档是否对你有帮助?