TRTCLiveRoom
is based on Tencent Real-Time Communication (TRTC) and Instant Messaging (IM). Its features include:
TRTCLiveRoom
is an open-source class depending on two closed-source Tencent Cloud SDKs. For the specific implementation process, please see Interactive Live Video Streaming (Flutter).
AVChatRoom
feature of the IM SDK is used to implement chat rooms, and IM messages are used to facilitate the co-anchoring process between anchors.TRTCLiveRoom
API OverviewAPI | Description |
---|---|
sharedInstance | Gets a singleton object. |
destroySharedInstance | Terminates a singleton object. |
registerListener | Registers an event listener. |
unRegisterListener | Removes an event listener. |
login | Logs in. |
logout | Logs out. |
setSelfProfile | Sets profile. |
API | Description |
---|---|
createRoom | Creates a room (called by anchor). If the room does not exist, the system will automatically create a room. |
destroyRoom | Closes a room (called by anchor). |
enterRoom | Enters a room (called by audience). |
exitRoom | Exits a room (called by audience). |
getRoomInfos | Gets room list details. |
getAnchorList | Gets the anchor and co-anchors in a room. This API works only if it is called after enterRoom() . |
getRoomMemberList | Gets the list of all users in a room. This API works only if it is called after enterRoom() . |
API | Description |
---|---|
startCameraPreview | Enables preview of local video. |
stopCameraPreview | Stops local video capturing and preview. |
startPublish | Starts live streaming (pushing streams). |
stopPublish | Stops live streaming (pushing streams). |
startPlay | Plays a remote video. This API can be called in common playback and co-anchoring scenarios. |
stopPlay | Stops rendering a remote video. |
API | Description |
---|---|
requestJoinAnchor | Requests co-anchoring (called by audience). |
responseJoinAnchor | Responds to a co-anchoring request (called by anchor). |
kickoutJoinAnchor | Removes a user from co-anchoring (called by anchor). |
API | Description |
---|---|
requestRoomPK | Requests cross-room competition (called by anchor). |
responseRoomPK | Responds to a cross-room competition request (called by anchor). |
quitRoomPK | Quits cross-room competition. |
API | Description |
---|---|
switchCamera | Switches between the front and rear cameras. |
setMirror | Sets the mirror mode. |
muteLocalAudio | Mutes/Unmutes the local user. |
muteRemoteAudio | Mutes/Unmutes a remote user. |
muteAllRemoteAudio | Mutes/Unmutes all remote users. |
API | Description |
---|---|
getAudioEffectManager | Gets the background music and audio effect management object TXAudioEffectManager. |
API | Description |
---|---|
getBeautyManager | Gets the beauty filter management object TXBeautyManager. |
API | Description |
---|---|
sendRoomTextMsg | Broadcasts a text message in a room. This API is generally used for on-screen comments. |
sendRoomCustomMsg | Sends a custom text message. |
API | Description |
---|---|
onError | Error |
onWarning | Warning |
onKickedOffline | You were kicked offline because another user logged in to the account. |
API | Description |
---|---|
onEnterRoom | You entered the room. |
onUserVideoAvailable | Whether a remote user has playable video in the primary stream (usually used for camera images) |
onRoomDestroy | The room was closed. |
API | Description |
---|---|
onAnchorEnter | An anchor entered the room. |
onAnchorExit | An anchor left the room. |
onAudienceEnter | A viewer entered the room. |
onAudienceExit | A viewer left the room. |
API | Description |
---|---|
onRequestJoinAnchor | A co-anchoring request was received. |
onAnchorAccepted | The co-anchoring request was accepted. |
onAnchorRejected | The co-anchoring request was rejected. |
onKickoutJoinAnchor | A user was removed from co-anchoring. |
API | Description |
---|---|
onRequestRoomPK | A cross-room competition request was received. |
onRoomPKAccepted | The cross-room competition request was accepted. |
onRoomPKRejected | The cross-room competition request was rejected. |
onQuitRoomPK | The cross-room competition ended. |
API | Description |
---|---|
onRecvRoomTextMsg | Receipt of a text message |
onRecvRoomCustomMsg | Receipt of a custom message |
This API is used to get a TRTCLiveRoom singleton object.
static Future<TRTCLiveRoom> sharedInstance()
This API is used to terminate a TRTCLiveRoom singleton object.
Note:After the instance is terminated, the externally cached
TRTCLiveRoom
instance can no longer be used. You need to call sharedInstance again to get a new instance.
static void destroySharedInstance()
This API is used to get the event callback of TRTCLiveRoom. You can use TRTCLiveRoomDelegate
to get various status notifications of TRTCLiveRoom.
void registerListener(VoiceListenerFunc func);
Note:
registerListener
is the delegate callback ofTRTCLiveRoom
.
This API is used to remove the event listener.
void unRegisterListener(VoiceListenerFunc func);
This API is used to log in.
Future<ActionCallback> login(
int sdkAppId, String userId, String userSig, TRTCLiveRoomConfig config);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
sdkAppId | int | You can view the SDKAppID via Application Management > Application Info in the TRTC console. |
userId | String | ID of the current user, which is a string that can contain only letters (a-z and A-Z), digits (0-9), hyphens (-), and underscores (_). |
userSig | String | Tencent Cloud's proprietary security signature. For how to obtain it, please see UserSig. |
config | TRTCLiveRoomConfig | Global configuration information, which needs to be initialized during login and cannot be modified afterwards.
|
This API is used to log out.
Future<ActionCallback> logout();
This API is used to set profile.
Future<ActionCallback> setSelfProfile(String userName, String avatarURL);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userName | String | Nickname |
avatarURL | String | Profile photo address |
This API is used to create a room (called by anchor).
Future<ActionCallback> createRoom(int roomId, TRTCCreateRoomParam roomParam);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
roomId | int | Room ID. You need to assign and manage the IDs in a centralized manner. Multiple roomId values can be aggregated into a live room list. Currently, Tencent Cloud does not provide list management services. Please manage the list on your own. |
roomParam | RoomParam | Room information, such as room name and cover information. If both the room list and room information are managed on your server, you can ignore this parameter. |
The process of creating a room and starting live streaming as an anchor is as follows:
startCameraPreview()
to enable camera preview and set beauty filters. createRoom()
to create a room, the result of which is returned via the ActionCallback
callback.starPublish()
to push streams.This API is used to close a room (called by anchor).
Future<ActionCallback> destroyRoom();
This API is used to enter a room (called by audience).
Future<ActionCallback> enterRoom(int roomId);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
roomId | int | Room ID |
The process of entering a room and starting playback as audience is as follows:
roomId
and other room information of multiple rooms.enterRoom()
to enter the room.startPlay(userId)
, passing in the anchor's userId
to start playback.userId
, the user can call startPlay(userId)
to start playback.userId
before room entry, he or she can find it in the onAnchorEnter(userId)
callback of TRTCLiveRoomDelegate
, which is returned after room entry. The user can then call startPlay(userId)
to start playback. This API is used to leave a room.
Future<ActionCallback> exitRoom();
This API is used to get room list details, which are set by anchors via roomInfo
when they call createRoom()
.
Note:You don't need this API if both the room list and room information are managed on your server.
Future<RoomInfoCallback> getRoomInfos(List<String> roomIdList);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
roomIdList | List<String> | Room ID list |
This API is used to get the anchor and co-anchors in a room. It takes effect only if it is called after enterRoom()
.
Future<UserListCallback> getAnchorList();
This API is used to get the information of all audience in the room. It takes effect only if it is called after enterRoom()
.
Future<UserListCallback> getRoomMemberList(int nextSeq)
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
nextSeq | int | Pulling-by-page flag. It is set to 0 when the information is pulled for the first time. If the callback succeeds and nextSeq is not 0, pagination is needed. The value of this field is passed in for the next pull until the value becomes 0. |
This API is used to enable local video preview.
Future<void> startCameraPreview(bool isFrontCamera, int viewId);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
isFrontCamera | bool | true : turns the front camera on; false : turns the rear camera on. |
viewId | int | Called back video view ID |
This API is used to stop local video capturing and preview.
Future<void> stopCameraPreview();
This API is used to start live streaming (pushing streams), which can be called in the following scenarios:
Future<void> startPublish(String streamId);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
streamId | String? | The streamId used to bind live streaming CDNs. You need to specify the streamId of the anchor if you want audience to play the anchor's streams via live streaming CDNs. |
This API is used to stop live streaming (pushing streams), which can be called in the following scenarios:
Future<void> stopPublish();
This API is used to play a remote video. It can be called in common playback and co-anchoring scenarios.
Future<void> startPlay(String userId, int viewId);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | ID of the user whose video is to be played |
viewId | int | Called back video view ID |
Common playback:
userId
, audience can call startPlay(userId)
to play the anchor's video after enterRoom()
.userId
before room entry, they can find it in the onAnchorEnter(userId)
callback of TRTCLiveRoomDelegate
, which is returned after room entry. They can then call startPlay(userId)
to play the anchor's video.Co-anchoring:
After co-anchoring is initiated, the anchor will receive the onAnchorEnter(userId)
callback from TRTCLiveRoomDelegate
and can call startPlay(userId), passing in the
userId` returned by the callback to play co-anchoring video.
This API is used to stop rendering remote video. It needs to be called when the onAnchorExit()
callback is received.
Future<void> stopPlay(String userId);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | ID of the remote user |
This API is used by audience to send a co-anchoring request.
Future<ActionCallback> requestJoinAnchor();
The process of co-anchoring between anchor and audience is as follows:
requestJoinAnchor()
to send a co-anchoring request to the anchor.onRequestJoinAnchor()
callback of TRTCLiveRoomDelegate
.responseJoinAnchor()
to accept or reject the co-anchoring request.responseCallback
callback, which carries the anchor's response.startCameraPreview()
to enable local camera preview.startPublish()
to push streams.onAnchorEnter()
callback of TRTCLiveRoomDelegate
.startPlay()
to play the co-anchoring viewer's video.onAnchorEnter()
callback and can call startPlay()
to play other co-anchoring viewers' video.This API is used by anchors to respond to a co-anchoring request after receiving the onRequestJoinAnchor()
callback of TRTCLiveRoomDelegate
.
Future<ActionCallback> responseJoinAnchor(String userId, boolean agreee);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | User ID of the viewer |
agree | bool | true : accepts; false : rejects. |
This API is used by anchors to remove a user from co-anchoring. After this API is called, the removed user will receive the onKickoutJoinAnchor()
callback of TRTCLiveRoomDelegate
.
Future<ActionCallback> kickoutJoinAnchor(String userId);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | ID of the co-anchoring user |
This API is used by anchors to request cross-room competition.
Future<ActionCallback> requestRoomPK(int roomId, String userId);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
roomId | int | Room ID of the anchor to call |
userId | String | User ID of the anchor to call |
Two anchors in different rooms can compete with each other. The process is as follows:
requestRoomPK()
to send a co-anchoring request to anchor B.onRequestRoomPK()
callback of TRTCLiveRoomDelegate
.responseRoomPK()
to respond to the competition request.onAnchorEnter()
callback of TRTCLiveRoomDelegate
and calls startPlay()
to play anchor A's video.onRoomPKAccepted
or onRoomPKRejected
callback.onAnchorEnter()
callback of TRTCLiveRoomDelegate
and calls startPlay()
to play anchor B's video.This API is used by anchors to respond to a cross-room competition request, after which the request sending anchor will receive the responseCallback
callback, which carries the response passed in to requestRoomPK
.
Future<ActionCallback> responseRoomPK(String userId, boolean agree);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | User ID of the anchor sending the competition request |
agree | bool | true : accepts; false : rejects. |
This API is used to quit cross-room competition. If either anchor quits cross-room competition, the other anchor will receive the onQuitRoomPk()
callback of TRTCLiveRoomDelegate
.
Future<ActionCallback> quitRoomPK();
This API is used to switch between the front and rear cameras.
Future<void> switchCamera(boolean isFrontCamera);
This API is used to set the mirror mode.
Future<void> setMirror(boolean isMirror);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
isMirror | bool | Enables/Disables mirroring. |
This API is used to mute or unmute the local user.
Future<void> muteLocalAudio(boolean mute);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
mute | boolean | true : mute; false : unmute |
This API is used to enable the hands-free mode.
Future<void> muteRemoteAudio(String userId, boolean mute);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | ID of the remote user |
mute | boolean | true : mute; false : unmute |
This API is used to mute or unmute all remote users.
Future<void> muteAllRemoteAudio(boolean mute);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
mute | boolean | true : mute; false : unmute |
This API is used to get the background music and audio effect management object TXAudioEffectManager.
getAudioEffectManager();
This API is used to get the beauty filter management object TXBeautyManager.
getBeautyManager();
You can do the following using TXBeautyManager
:
This API is used to broadcast a text message in a room, which is generally used for on-screen comments.
Future<ActionCallback> sendRoomTextMsg(String message);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
message | String | Text message |
This API is used to send a custom text message.
Future<ActionCallback> sendRoomCustomMsg(String cmd, String message);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
cmd | String | Custom command word used to distinguish between different message types |
message | String | Text message |
TRTCLiveRoomDelegate
Event Callback APIsCallback for error.
Note:This callback indicates that the SDK encountered an unrecoverable error. Such errors must be listened for, and UI reminders should be sent to users if necessary.
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
errCode | int | Error code |
errMsg | String | Error message |
Callback for warning.
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
warningCode | int | Warning code |
warningMsg | String | Warning message |
Callback for being kicked offline because another user logged in to the same account.
Callback for room closing. All users in the room will receive this callback after the anchor leaves the room.
Callback for room entry by the local user.
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
result | int | If result is greater than 0, it indicates the time (ms) room entry took; if result is less than 0, it represents the error code for room entry. |
Callback for whether a remote user has playable video in the primary stream (usually used for camera images).
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | User ID |
available | boolean | Whether the user's video is enabled |
Callback for a new anchor/co-anchoring viewer. Audience will receive this callback when there is a new anchor/co-anchoring viewer in the room and can call startPlay()
of TRTCLiveRoom
to play the video of the anchor/co-anchoring viewer.
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | User ID of the new anchor/co-anchoring viewer |
userName | String | Username |
userAvatar | String | Profile photo address |
Callback for the quitting of a competing anchor/co-anchoring viewer. The anchor and co-anchoring viewers in a room will receive this callback after a competing anchor/co-anchoring viewer quits co-anchoring and can call stopPlay()
of TRTCLiveRoom
to stop playing the video of the competing anchor/co-anchoring viewer.
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | ID of the user who quitted co-anchoring |
userName | String | Username |
userAvatar | String | Profile photo address |
Callback for room entry by a viewer.
void onAudienceEnter(TRTCLiveRoomDef.TRTCLiveUserInfo userInfo);
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userInfo | TRTCLiveRoomDef.TRTCLiveUserInfo | Information of the viewer who enters the room, such as user ID, nickname, and profile photo. |
Callback for room exit by a viewer.
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | User ID of the viewer |
userName | String | Username |
userAvatar | String | Profile photo address |
Callback for receiving a co-anchoring request from a viewer.
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | ID of the request sending user |
userName | String | Username |
userAvatar | String | Profile photo address |
Callback for accepting a co-anchoring request.
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | User ID of the anchor |
Callback for rejecting a co-anchoring request.
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | User ID of the anchor |
Callback for being removed from co-anchoring. A co-anchoring viewer needs to call stopPublish()
of TRTCLiveRoom
to quit co-anchoring after receiving this callback.
Callback for receiving a cross-room competition request. If an anchor accepts the request after receiving this callback, he or she should wait for the onAnchorEnter()
callback of TRTCLiveRoomDelegate
and call startPlay()
to play the other anchor's video.
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | User ID of the request sending anchor |
userName | String | Username |
userAvatar | String | Profile photo address |
Callback for accepting a cross-room competition request.
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | User ID of the anchor who accepted the request |
Callback for rejecting a cross-room competition request.
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
userId | String | User ID of the anchor who rejected the request |
Callback for ending cross-room competition.
A text message was received.
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
message | String | Text message |
A custom message was received.
The parameters are as detailed below:
Parameter | Type | Description |
---|---|---|
command | String | Custom command word used to distinguish between different message types |
message | String | Text message |
Was this page helpful?