Documentation
TalkingRoom Live Audio Room
Documentation
Demo APP
SDK Center
API Center
FAQ
Code Market
Console
Sign Up
Log In
中文站 English
  • Documentation
  • Live Audio Room
  • Getting started

Build a live audio room

Last updated:2022-04-11 20:08

This document describes how to use the ZegoLiveAudioRoom SDK to build a live audio room.


Prerequisites

  • Contact us to activate the Live Audio Room service.
  • Create a project in ZEGOCLOUD Admin Console.

Understand the process

The following diagram shows the basic process of creating a live audio room and taking speaker seats to speak:

/Pics/ZIMChatRoom/ZIMChatRoom_process_EN.png

Integrate the ZegoLiveAudioRoom SDK

To integrate the SDK automatically with CocoaPods, do the following:

  1. Download the Sample codes, and copy the ZegoLiveAudioRoom folder to your project directory (if you have no project, create a new one).

/Pics/ZIMChatRoom/build_and_run14.png

  1. Add the Podfile file to your project directory.

    pod 'ZIM'
    pod 'ZegoExpressEngine/Audio'

/Pics/ZIMChatRoom/build_and_run14.png

  1. Open Terminal, run the install command.

    pod install

Add permissions

Permissions can be set as needed.

  1. Open Xcode, select the target object, and then click Info > Custom iOS Target Properties.
  2. Click the Add button (+) to add microphone permissions.

/Pics/ZIMChatRoom/set_properties.png

Initialize the ZegoLiveAudioRoom SDK

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

Log in

To access the signaling service of Live Audio Room with the ZegoLiveAudioRoom SDK, you must log in first.

  • For business security, you will need to provide a token for the ZIM SDK to validate the login privilege. For details, see Authentication.
  • For debugging, you can refer to our Sample code to generate tokens on your app client.
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. 
        }

Create/Join a live audio room

  • You become a Host after creating a live audio room, and you owe more permissions, such as closing untaken speaker seats, removing a specified listener from the speaker seat, etc.
  • You become a Listener after joining a live audio room, you can take a speaker seat to be a speaker or leave the speaker seat to become a listener again.
  • To prevent listeners in a room from being able to speak directly without taking a speaker seat, you will need to provide a token for the RTC SDK to validate whether you have the privileges to create or join a room. For details, see Use Tokens for authentication.
  • This Token can be the same as the Token you provided for login.

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. 
        }

Send/Receive messages in the 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. 
    }

Take a speaker seat

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. 
    }

Renew a token

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)

}

Leave a speaker seat

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.
    }

Leave a live audio room

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. 
        }

Log out

To finish the Live Audio Room signaling service, call the logout method.

RoomManager.shared.userService.logout()

Deinitialize the ZegoLiveAudioRoom SDK

To deinitialize the SDK to make it uninitialized, call the uninit method.

RoomManager.shared.uninit()
Page Directory
  • Free trial
  • 提交工单
    咨询集成、功能及报价等问题
    电话咨询
    400 1006 604
    Get Consulting
    Scan Wechat QR code