TUIVoiceRoom
is an open-source audio/video UI component. After integrating it into your project, you can make your application support the group audio chat scenario simply by writing a few lines of code. It also supports the Android platform. Its basic features are as shown below: |
TUIVoiceRoom
componentTUIVoiceRoom
folder at the same level as the Podfile
in your Xcode project, copy TXAppBasic, Resources, Source, and TUIVoiceRoom.podspec files from the iOS
directory in the GitHub repository to the folder, and complete the following import operations:Podfile
and import TUIVocieRoom.podspec
as follows:# `path` is the path of `TXAppBasic.podspec` relative to the `Podfile`pod 'TXAppBasic', :path => "TUIVoiceRoom/TXAppBasic/"# `path` is the path of `TUIVoiceRoom.podspec` relative to the `Podfile`pod 'TUIVoiceRoom', :path => "TUIVoiceRoom/", :subspecs => ["TRTC"]
Podfile
, and run pod install
.pod install
info.plist
, add Privacy > Microphone Usage Description
to request mic access.<key>NSMicrophoneUsageDescription</key><string>`VoiceRoomApp` needs to access your mic to be able to shoot videos with audio.</string>
// Initialize TUIKitlet mTRTCVoiceRoom = TRTCVoiceRoom.shared()// Log inmTRTCVoiceRoom.login(sdkAppID: SDKAppID, userId: userId, userSig: userSig) { code, message inif code == 0 {// Logged in}}
SDKAppID
information is as shown in the figure below:
SDKAppID
. The SecretKey information is shown in the above figure.SDKAppID
, userId
, and Secretkey
. You can click here to directly generate a debugging userSig
online. For more information, see UserSig.// Initialize the audio chat room parameterslet roomParam = VoiceRoomParam()roomParam.roomName = "Room name"roomParam.needRequest = false // Whether the room owner’s permission is required for listeners to speakroomParam.coverUrl = "URL of room cover image"roomParam.seatCount = 7 // Number of room seats. In this example, the number is 7. 6 seats are available after you take one.roomParam.seatInfoList = []// Initialize the seat informationfor _ in 0..< param.seatCount {let seatInfo = VoiceRoomSeatInfo()param.seatInfoList.append(seatInfo)}// Create a roommTRTCVoiceRoom.createRoom(roomID: yourRoomID, roomParam: roomParam) { (code, message) inif code == 0 {// Group created successfully}}
// 1. A listener calls an API to enter the roommTRTCVoiceRoom.enterRoom(roomID: roomID) { (code, message) in// Callback of the room entry resultif code == 0 {// Entered room successfully}}
// 1. A listener calls an API to mic onlet seatIndex = 2; // Seat indexmTRTCVoiceRoom.enterSeat(seatIndex: 2) { (code, message) inif code == 0 {// Mic turned on successfully}}// 2. The `onSeatListChange` callback is received, and the seat list is refreshed@Overridefunc onSeatListChange(seatInfoList: [VoiceRoomSeatInfo]) {// Refreshed seat list}
// 1. The room owner makes a listener a speakerlet seatIndex = 2; // Seat indexlet userId = "123"; // ID of the user to speakmTRTCVoiceRoom.pickSeat(seatIndex: 1, userId: "123") { (code, message) inif code == 0 {}}// 2. The `onSeatListChange` callback is received, and the seat list is refreshedfunc onSeatListChange(seatInfoList: [VoiceRoomSeatInfo]) {// Refreshed seat list}
// Listener// 1. A listener calls an API to request to speaklet seatIndex = "1"; // Seat indexlet userId = "123"; // User IDlet inviteId = mTRTCVoiceRoom.sendInvitation(cmd: "takeSeat", userId: ownerUserId, content: "1") { (code, message) in// Callback of the result}// 2. Place the user in the seat after the invitation is acceptedfunc onInviteeAccepted(identifier: String, invitee: String) {if identifier == selfID {self.mTRTCVoiceRoom.enterSeat(seatIndex: ) { (code, message) in// Callback of the result}}}// Room owner// 1. The room owner receives the requestfunc onReceiveNewInvitation(identifier: String, inviter: String, cmd: String, content: String) {if cmd == "takeSeat" {// 2. The room owner accepts the requestself.mTRTCVoiceRoom.acceptInvitation(identifier: identifier, callback: nil)}}
// Room owner// 1. Call `sendInvitation` to invite user `123` to take seat 2let inviteId = self.mTRTCVoiceRoom.sendInvitation(cmd: "pickSeat", userId: ownerUserId, content: "2") { (code, message) in// Callback of the result}// 2. Place the user in the seat after the invitation is acceptedfunc onInviteeAccepted(identifier: String, invitee: String) {if identifier == selfID {self.mTRTCVoiceRoom.pickSeat(seatIndex: ) { (code, message) in// Callback of the result}}}// Listener// 1. The listener receives the invitationfunc onReceiveNewInvitation(identifier: String, inviter: String, cmd: String, content: String) {if cmd == "pickSeat" {// 2. The listener accepts the invitationself.mTRTCVoiceRoom.acceptInvitation(identifier: identifier, callback: nil)}}
// Sender: Sends text chat messagesself.mTRTCVoiceRoom.sendRoomTextMsg(message: message) { (code, message) in}// Receiver: Listens for text chat messagesfunc onRecvRoomTextMsg(message: String, userInfo: VoiceRoomUserInfo) {// Handling of the messages received}
// For example, a sender can customize commands to distinguish on-screen comments and likes.// For example, use "CMD_DANMU" to indicate on-screen comments and "CMD_LIKE" to indicate likes.self.mTRTCVoiceRoom.sendRoomCustomMsg(cmd: "CMD_DANMU", message: "hello world", callback: nil)self.mTRTCVoiceRoom.sendRoomCustomMsg(cmd: "CMD_LIKE", message: "", callback: nil)// Receiver: Listens for custom messagesfunc onRecvRoomCustomMsg(cmd: String, message: String, userInfo: VoiceRoomUserInfo) {if cmd == "CMD_DANMU" {// An on-screen comment is received}if cmd == "CMD_LIKE" {// A like is received}}
Was this page helpful?