This document describes how to use the ZegoLiveAudioRoom SDK to build a live audio room.
The following diagram shows the basic process of creating a live audio room and taking speaker seats to speak:
To integrate the SDK automatically with CocoaPods, do the following:
ZegoLiveAudioRoom
folder to your project directory (if you have no project, create a new one). Add the Podfile
file to your project directory.
pod 'ZIM'
pod 'ZegoExpressEngine/Audio'
Open Terminal, run the install
command.
pod install
Permissions can be set as needed.
To initialize the ZegoLiveAudioRoom SDK, get the RoomManager
instance, pass the AppID of your project.
// Initialize the SDK. We recommend you call this method when the application starts.
// YOUR_APP_ID is the AppID you get from ZEGOCLOUD Admin Console.
RoomManager.shared.initWithAppID(appID: YOUR_APP_ID) { result in
};
To receive callbacks, set the corresponding delegate
to self
, or call the addUserServiceDelegate
method to listen for and handle event callbacks as needed.
RoomManager.shared.roomService.delegate = self
RoomManager.shared.userService.addUserServiceDelegate(self)
RoomManager.shared.speakerService.delegate = self
RoomManager.shared.messageService.delegate = self
RoomManager.shared.giftService.delegate = self
To access the signaling service of Live Audio Room with the ZegoLiveAudioRoom
SDK, you must log in first.
let userInfo = UserInfo("YOUR_USER_ID", "YOUR_USER_ID", .listener)
// Your token
let token: String = "xxx"
RoomManager.shared.userService.login(userInfo, token) {
result in
// Callback for the login result.
}
To create a live audio room, call the createRoom
method:
RoomManager.shared.roomService.createRoom("YOUR_ROOM_ID", "YOUR_ROOM_NAME", token) { result in
// Callback for the result of create a live audio room.
}
To join a live audio room, call the joinRoom
method:
RoomManager.shared.roomService.joinRoom("YOUR_ROOM_ID", token) { result in
// Callback for the result of join a live audio room.
}
In a live audio room, both Host and Listeners can send and receive messages.
To send messages, call the sendTextMessage
method with the message content.
RoomManager.shared.messageService.sendTextMessage("YOUR_MESSAGE") { result in
// Callback for the result of send a message.
}
}
To receive messages, listen for the receiveTextMessage
callback.
func receiveTextMessage(_ message: TextMessage) {
// This callback will be triggered when new messages are received.
}
To take a speaker seat to talk, call the takeSeat
method. And the SDK publishes streams simultaneously.
RoomManager.shared.speakerService.takeSeat(0, callback:{ result in
// Callback for the result of take a speaker seat.
})
When there is a new listener takes a seat and becomes a speaker, all participants in the room receive notifications through the updateSpeakerSeats
callback. You can set up a UI refresh action for this callback as needed.
func updateSpeakerSeats(_ seatDict: [String:Any]?) {
// This callback will be triggered when the status of speaker seats changes.
}
30 seconds before a Token expires, the SDK sends out a notification through the onRoomTokenWillExpire
callback.
Upon receiving this callback, you need to get a new Token from your app server first, and then pass the new token to the renewToken
method.
func onRoomTokenWillExpire(_ remainTimeInSecond: Int32, roomID: String?) {
let token: String = xxxxx ///new token
RoomManager.shared.roomService.renewToken(token, roomID: roomID)
}
To leave a speaker seat and become a listener again, call the leaveSeat
method. And the SDK stops publishing streams simultaneously.
RoomManager.shared.speakerService.leaveSeat { Result in
switch Result {
// Callback for the result of leave a speaker seat.
}
When there is an existing speaker leaves a seat and becomes a listener again, all participants in the room receive notifications through the updateSpeakerSeats
callback. You can set up a UI refresh action for this callback as needed.
func updateSpeakerSeats(_ seatDict: [String:Any]?) {
// This callback will be triggered when the status of speaker seats changes.
}
To leave the live audio room, call the leaveRoom
method. And the SDK stops all the stream publishing and playing operations simultaneously.
RoomManager.shared.roomService.leaveRoom { Result in
// Callback for the result of leave a live audio room.
}
To finish the Live Audio Room signaling service, call the logout
method.
RoomManager.shared.userService.logout()
To deinitialize the SDK to make it uninitialized, call the uninit
method.
RoomManager.shared.uninit()