Audio Broadcasting and Listening

This document mainly introduces how to use the RTC Room Engine SDK to implement the voice chat broadcasting feature.

Prerequisites

Before using the RTC RoomEngine SDK, you need to call the SDK login to ensure the subsequent features work properly.

Audio Broadcasting

Using the RTC Room Engine to implement voice chat broadcasting involves three core steps: creating and joining a live room, going on mic to stream, and enabling media devices. The following sections will provide a detailed introduction.

Step 1: Creating and Joining a Live Room

iOS
Android
Before broadcasting, you need to fill in the key parameters TUIRoomInfo. The following sections will provide a detailed introduction:

Parameters: TUIRoomInfo

TUIRoomInfo consists of many fields, but usually, you only need to focus on filling in the following fields:
Parameter Name
Field Description
Additional Notes
Data Type
Example
roomId
Room ID
Only letters (a-z, A-Z), digits (0-9), underscores, and hyphens are allowed.
This field is a required parameter when creating a room and can contain up to 48 bytes.
New Character String
"live_100001"
name
Room Name
Room name of string type. (If not provided, it will be the same as roomId)
New Character String
"denny`s room"
seatMode

Microphone Mode

This field is effective only when microphone control is enabled.
It is divided into "freeToTake" and "applyToTake" modes. In freeToTake mode, audience members can take the microphone freely without applying. In applyToTake mode, audience members need the room owner's approval to take the microphone.


Enumeration Value
TUISeatMode.applyToTake
maxSeatCount
Maximum Number of Microphones
The maximum number of mic positions of numeric type, referring to the maximum number of people on mic simultaneously, maxSeatCount.
The maximum number of seats is consistent with the maximum number of participants in the package you purchased.
Digits
10
isSeatEnabled
Whether to enable microphone position control
Boolean type mic control enable flag.
Boolean value
true
roomType
Room type
Divided into two room types: "conference" and "live". Voice chat belongs to live, so use live here.
Enumeration Value
TUIRoomType.live
After preparing the parameters TUIRoomInfo, you can call the createRoom and enterRoom API functions to create and join a live room.
Before broadcasting, you need to fill in the key parameters RoomInfo. The following sections will provide a detailed introduction:

Parameters: RoomInfo

RoomInfo consists of many fields, but usually, you only need to focus on filling in the following fields:
Parameter Name
Field Description
Additional Notes
Data Type
Example
roomId
Room ID
Only letters (a-z, A-Z), digits (0-9), underscores, and hyphens are allowed.
This field is a required parameter when creating a room and can contain up to 48 bytes.
New Character String
"live_100001"
name
Room Name
Room name of string type. (If not provided, it will be the same as roomId)
New Character String
"denny`s room"
seatMode

Microphone Mode

This field is effective only when microphone control is enabled.
It is divided into "FREE_TO_TAKE" and "APPLY_TO_TAKE" modes. In FREE_TO_TAKE mode, audience members can take the microphone freely without applying. In APPLY_TO_TAKE mode, audience members need the room owner's approval to take the microphone.


Enumeration Value
SeatMode.APPLY_TO_TAKE
maxSeatCount
Maximum Number of Microphones
The maximum number of mic positions of numeric type, referring to the maximum number of people on mic simultaneously, maxSeatCount.
The maximum number of seats is consistent with the maximum number of participants in the package you purchased.
Digits
10
isSeatEnabled
Whether to enable microphone position control
Boolean type mic control enable flag.
Boolean value
true
roomType
Room type
Divided into two room types: "CONFERENCE" and "LIVE". Voice chat belongs to live, so use live here.
Enumeration Value
RoomType.Live

After preparing the parameters RoomInfo, you can call the createRoom and enterRoom API functions to create and join a live room.
iOS
Android
import RTCRoomEngine

let roomInfo = TUIRoomInfo()
roomInfo.roomId = "voice_100001" // Please replace it with your own room ID.
roomInfo.name = "denny`s room" // Please replace it with your own room name.
roomInfo.seatMode = .applyToTake // You can also replace it with .freeToTake according to your needs.
roomInfo.maxSeatCount = 10 // Please replace it with the maximum number of seats in the Live package you purchased.
roomInfo.isSeatEnabled = true // If you do not need seat control, you can set isSeatEnabled to false
roomInfo.roomType = .live // Ensure the room type is live.

TUIRoomEngine.sharedInstance().createRoom(roomInfo) { [weak self] in
guard let self = self else { return }
TUIRoomEngine.sharedInstance.enterRoom(self.roomId, roomType: .live) { [weak self] roomInfo in
guard let self = self else { return }
// Successfully joined the live streaming room
} onError: { code, message in
// Failed to join the live streaming room
}
} onError: { code, message in
// Failed to create the live streaming room
}
TUIRoomDefine.RoomInfo roomInfo = new TUIRoomDefine.RoomInfo();
roomInfo.roomId = "voice_100001"; // Please replace it with your own room ID.
roomInfo.name = "denny`s room"; // Please replace it with your own room name.
roomInfo.seatMode = TUIRoomDefine.SeatMode.APPLY_TO_TAKE; // You can also replace it with .freeToTake according to your needs.
roomInfo.maxSeatCount = 10; // Please replace it with the maximum number of seats in the Live package you purchased.
roomInfo.isSeatEnabled = true; // If you do not need seat control, you can set isSeatEnabled to false
roomInfo.roomType = TUIRoomDefine.RoomType.LIVE; // Ensure the room type is live.

TUIRoomEngine.sharedInstance().createRoom(roomInfo, new TUIRoomDefine.ActionCallback() {
@Override
public void onSuccess() {
TUIRoomEngine.sharedInstance().enterRoom(roomInfo.roomId, TUIRoomDefine.RoomType.LIVE, new TUIRoomDefine.GetRoomInfoCallback() {
@Override
public void onSuccess(TUIRoomDefine.RoomInfo roomInfo) {
// Successfully joined the live streaming room
}
@Override
public void onError(TUICommonDefine.Error error, String message) {
// Failed to join the live streaming room
}
});
}
@Override
public void onError(TUICommonDefine.Error error, String message) {
// Failed to create the live streaming room
}
});

Step 2: Pushing Stream to the Stage

You can start streaming only after taking the mic. Therefore, after successfully creating the room, you need to call the takeSeat API to take the mic and start streaming.
iOS
Android
import RTCRoomEngine

let index = 0 // Please replace this with the seat number you want to request
let timeout = 30 // Please replace this with the timeout duration for your speaking request, in seconds. If set to 0, the SDK will not perform timeout detection or trigger timeout callbacks.

TUIRoomEngine.sharedInstance().takeSeat(index, timeout: TimeInterval(timeout)) { requestId, userId in
// Took the mic successfully
} onRejected: { requestId, userId, messagae in
// Seat request rejected
} onCancelled: { requestId, userId in
// Seat request canceled
} onTimeout: { requestId, userId in
// Seat request timed out
} onError: { requestId, userId, code, message in
// Failed to join the microphone
}
int index = 0; // Please replace this with the seat number you want to request
int timeout = 30; // Replace this with the timeout duration for requesting to speak, in seconds. If set to 0, the SDK will not perform timeout detection or trigger timeout callbacks

TUIRoomEngine.sharedInstance().takeSeat(index, timeout, new TUIRoomDefine.RequestCallback() {
@Override
public void onAccepted(String requestId, String userId) {
// Took the mic successfully
}
@Override
public void onRejected(String requestId, String userId, String message) {
// Seat request rejected
}

@Override
public void onCancelled(String requestId, String userId) {
// Seat request canceled
}
@Override
public void onTimeout(String requestId, String userId) {
// Seat request timed out
}
@Override
public void onError(String requestId, String userId, TUICommonDefine.Error error, String message) {
// Failed to join the microphone
}
});

Step 3: Enabling Media Devices

iOS
Android
After starting the broadcast, you also need to call the openLocalMicrophone API to turn on the microphone, passing in a parameter of type TUIAudioQuality quality to ensure the audience can hear your voice.
TUIAudioQuality is an enumeration.
Parameter Name
Field Description
speech
Voice mode. Mono; audio bitrate: 18kbps; suitable for voice call scenarios.
default
Default mode. Mono; audio bitrate: 50kbps; the default audio quality of the SDK, recommended if there are no special requirements.
music
Music mode. Stereo + full band; audio bitrate: 128kbps; suitable for scenarios requiring high-fidelity music transmission, such as online karaoke and music live streaming.
After starting the broadcast, you also need to call the openLocalMicrophone API to turn on the microphone, passing in a parameter of type AudioQuality quality to ensure the audience can hear your voice.
AudioQuality is an enumeration.
Parameter Name
Field Description
SPEECH
Voice mode. Mono; audio bitrate: 18kbps; suitable for voice call scenarios.
DEFAULT
Default mode. Mono; audio bitrate: 50kbps; the default audio quality of the SDK, recommended if there are no special requirements.
MUSIC
Music mode. Stereo + full band; audio bitrate: 128kbps; suitable for scenarios requiring high-fidelity music transmission, such as online karaoke and music live streaming.
Below is an example of turning on the local microphone in music mode.
iOS
Android
import RTCRoomEngine

let audioQuality: TUIAudioQuality = .music // Please select the corresponding mode based on your audio quality needs.
TUIRoomEngine.sharedInstance().openLocalMicrophone(audioQuality) {
// Successfully turned on the mic
} onError: { code, message in
// Failed to turn on the mic
}
TUIRoomDefine.AudioQuality audioQuality = TUIRoomDefine.AudioQuality.MUSIC; // Please select the corresponding mode based on your audio quality needs.
TUIRoomEngine.sharedInstance().openLocalMicrophone(audioQuality, new TUIRoomDefine.ActionCallback() {
@Override
public void onSuccess() {
// Successfully turned on the mic
}
@Override
public void onError(TUICommonDefine.Error error, String message) {
// Failed to turn on the mic
}
});

Audio Listening

You only need to call the enterRoom API to successfully enter the room and listen to the voice chat room host.
For enterRoom, you need to pass in two parameters: the room ID of the host you want to listen to and the room type.
Note:
There are two room types: conference and live. Voice chat rooms belong to live rooms, so when you call enterRoom to enter a voice chat room, the room type should be set to live.
iOS
Android
import RTCRoomEngine

let roomId = "voice_100001" // Room ID to listen to
let roomType = .live // Set this to .live

TUIRoomEngine.sharedInstance().enterRoom(roomId, roomType: roomType) { roomInfo in
// Entered room successfully
} onError: { code, message in
// Failed to enter the room
}
String roomId = "voice_100001"; // Room ID to listen to
TUIRoomDefine.RoomType roomType = TUIRoomDefine.RoomType.LIVE; // Set this to .live

TUIRoomEngine.sharedInstance().enterRoom(roomId, roomType, new TUIRoomDefine.GetRoomInfoCallback() {
@Override
public void onSuccess(TUIRoomDefine.RoomInfo roomInfo) {
// Entered room successfully
}
@Override
public void onError(TUICommonDefine.Error error, String message) {
// Failed to enter the room
}
});