Podfile
文件同一级目录下创建 TUIVoiceRoom
文件夹,将 Github仓库 iOS 目录 下的 TXAppBasic、Resources、Source、TUIVoiceRoom.podspec 等文件拷贝至您在自己工程创建的 TUIVoiceRoom
目录下。并完成如下导入动作:# path 为TXAppBasic.podspec相对于Podfile文件的相对路径pod 'TXAppBasic', :path => "TUIVoiceRoom/TXAppBasic/"# path 为TUIVoiceRoom.podspec相对于Podfile文件的相对路径pod 'TUIVoiceRoom', :path => "TUIVoiceRoom/", :subspecs => ["TRTC"]#For iOS 13.0 projects or later, you need to include the following line of code:post_install do |installer|installer.pods_project.targets.each do |target|target.build_configurations.each do |config|config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = 13.0endendend
pod install
,参考如下:pod install
Privacy > Microphone Usage Description
申请麦克风权限。<key>NSMicrophoneUsageDescription</key><string>VoiceRoomApp需要访问您的麦克风权限,开启后录制的视频才会有声音</string>
#define SDKAppID 000000000[[TRTCVoiceRoom sharedInstance] login:SDKAppID userId:@"userId" userSig:@"userSig" callback:^(int code, NSString * _Nonnull message) {if (code == 0) {NSLog(@"初始化成功");}}];
// 初始化let mTRTCVoiceRoom = TRTCVoiceRoom.shared()// TRTC 应用IDlet SDKAppID = 0// 登录mTRTCVoiceRoom.login(sdkAppID: SDKAppID, userId: "userId", userSig: "userSig") { code, message inif code == 0 {//登录成功}}
VoiceRoomParam *roomParam = [[VoiceRoomParam alloc] init];roomParam.roomName = @"房间名称";// 听众上麦是否需要房主同意roomParam.needRequest = NO;roomParam.coverUrl = @"房间封面图的 URL 地址";// 房间座位数,这里一共7个座位,房主占了一个后听众剩下6个座位roomParam.seatCount = 7;// 初始化麦位信息NSMutableArray *seatInfoArray = [NSMutableArray array];for (NSInteger i = 0; i < roomParam.seatCount; i++) {VoiceRoomSeatInfo *seatInfo = [[VoiceRoomSeatInfo alloc] init];[seatInfoArray addObject:seatInfo];}roomParam.seatInfoList = seatInfoArray;// 房主端创建房间[[TRTCVoiceRoom sharedInstance] createRoom:0 roomParam:roomParam callback:^(int code, NSString * _Nonnull message) {if (code == 0) {NSLog(@"创建房间成功");}}];
// 初始化语聊房参数let roomParam = VoiceRoomParam()roomParam.roomName = "房间名称"roomParam.needRequest = false // 听众上麦是否需要房主同意roomParam.coverUrl = "房间封面图的 URL 地址"roomParam.seatCount = 7 // 房间座位数,这里一共7个座位,房主占了一个后听众剩下6个座位roomParam.seatInfoList = []// 初始化麦位信息for _ in 0..< roomParam.seatCount {let seatInfo = VoiceRoomSeatInfo()roomParam.seatInfoList.append(seatInfo)}// 房主端创建房间TRTCVoiceRoom.shared().createRoom(roomID: yourRoomID, roomParam: roomParam) { (code, message) inif code == 0 {// 创建成功}}
// 1.听众调用加入房间[[TRTCVoiceRoom sharedInstance] enterRoom:0 callback:^(int code, NSString * _Nonnull message) {if (code == 0) {NSLog(@"进入房间成功");}}];
// 1.听众调用加入房间mTRTCVoiceRoom.enterRoom(roomID: roomID) { (code, message) in// 进入房间结果回调if code == 0 {// 进房成功}}
// 1: 听众调用上麦NSInteger seatIndex = 2; // 观众要上的麦位 index[[TRTCVoiceRoom sharedInstance] enterSeat:seatIndex callback:^(int code, NSString * _Nonnull message) {if (code == 0) {NSLog(@"上麦成功");}}];// 2.收到 onSeatListChange 回调,刷新您的麦位列表- (void)onSeatInfoChange:(NSArray<VoiceRoomSeatInfo *> *)seatInfolist {// 刷新的麦位列表}
// 1: 听众调用上麦let seatIndex = 2; //麦位的indexmTRTCVoiceRoom.enterSeat(seatIndex: seatIndex) { (code, message) inif code == 0 {// 上麦成功}}// 2.收到 onSeatListChange 回调,刷新您的麦位列表@Overridefunc onSeatListChange(seatInfoList: [VoiceRoomSeatInfo]) {// 刷新的麦位列表}
// 1: 房主调用抱人麦位NSInteger seatIndex = 2; // 观众要上的麦位 indexNSString *userId = @"123"; //需要上麦的用户id[[TRTCVoiceRoom sharedInstance] pickSeat:2 userId:@"user id" callback:^(int code, NSString * _Nonnull message) {if (code == 0) {NSLog(@"抱人上麦成功");}}];// 2.收到 onSeatListChange 回调,刷新您的麦位列表- (void)onSeatInfoChange:(NSArray<VoiceRoomSeatInfo *> *)seatInfolist {// 刷新的麦位列表}
// 1: 房主调用抱人麦位let seatIndex = 2; //麦位的indexlet userId = "123"; //需要上麦的用户idmTRTCVoiceRoom.pickSeat(seatIndex: seatIndex, userId: "123") { (code, message) inif code == 0 {}}// 2.收到 onSeatListChange 回调,刷新您的麦位列表func onSeatListChange(seatInfoList: [VoiceRoomSeatInfo]) {// 刷新的麦位列表}
// 听众端视角// 1.听众调用申请上麦NSString *seatIndex = @"1"; //麦位的indexNSString *userId = @"123"; //用户idNSString *inviteId = [[TRTCVoiceRoom sharedInstance] sendInvitation:@"takeSeat" userId:userId content:seatIndex callback:^(int code, NSString * _Nonnull message) {}];// 2.收到邀请的同意请求, 正式上麦- (void)onInviteeAccepted:(NSString *)identifierinvitee:(NSString *)invitee {if ([identifier isEqualToString:inviteId]) {[[TRTCVoiceRoom sharedInstance] enterSeat:1 callback:^(int code, NSString * _Nonnull message) {}];}}// 房主端视角// 1.房主收到请求- (void)onReceiveNewInvitation:(NSString *)identifierinviter:(NSString *)invitercmd:(NSString *)cmdcontent:(NSString *)content {if ([cmd isEqualToString:@"takeSeat"]) {// 2.房主同意听众请求[[TRTCVoiceRoom sharedInstance] acceptInvitation:identifier callback:^(int code, NSString * _Nonnull message) {}];}}
// 听众端视角// 1.听众调用申请上麦let seatIndex = "1"; //麦位的indexlet userId = "123"; //用户idlet inviteId = mTRTCVoiceRoom.sendInvitation(cmd: "takeSeat", userId: ownerUserId, content: "1") { (code, message) in// 发送结果回调}// 2.收到邀请的同意请求, 正式上麦func onInviteeAccepted(identifier: String, invitee: String) {if identifier == selfID {self.mTRTCVoiceRoom.enterSeat(seatIndex: 1) { (code, message) in// 上麦结果回调}}}// 房主端视角// 1.房主收到请求func onReceiveNewInvitation(identifier: String, inviter: String, cmd: String, content: String) {if cmd == "takeSeat" {// 2.房主同意听众请求self.mTRTCVoiceRoom).acceptInvitation(identifier: identifier, callback: nil)}}
// 房主端视角// 1.房主调用 sendInvitation,请求抱听众“123”上2号麦NSString *inviteId = [[TRTCVoiceRoom sharedInstance] sendInvitation:@"pickSeat" userId:@"123" content:@"2" callback:^(int code, NSString * _Nonnull message) {}];// 2.收到邀请的同意请求, 正式上麦- (void)onInviteeAccepted:(NSString *)identifierinvitee:(NSString *)invitee {if ([identifier isEqualToString:inviteId]) {[[TRTCVoiceRoom sharedInstance] pickSeat:2 userId:invitee callback:^(int code, NSString * _Nonnull message) {}];}}// 听众端视角// 1.听众收到请求- (void)onReceiveNewInvitation:(NSString *)identifierinviter:(NSString *)invitercmd:(NSString *)cmdcontent:(NSString *)content {if ([cmd isEqualToString:@"pickSeat"]) {[[TRTCVoiceRoom sharedInstance] acceptInvitation:identifier callback:^(int code, NSString * _Nonnull message) {}];}}
// 房主端视角// 1.房主调用 sendInvitation,请求抱听众“123”上2号麦let inviteId = self.mTRTCVoiceRoom.sendInvitation(cmd: "pickSeat", userId: ownerUserId, content: "2") { (code, message) in// 发送结果回调}// 2.收到邀请的同意请求, 正式上麦func onInviteeAccepted(identifier: String, invitee: String) {if identifier == selfID {self.mTRTCVoiceRoom.pickSeat(seatIndex:2, userId:invitee) { (code, message) in// 上麦结果回调}}}// 听众端视角// 1.听众收到请求func onReceiveNewInvitation(identifier: String, inviter: String, cmd: String, content: String) {if cmd == "pickSeat" {// 2.听众同意房主请求self.mTRTCVoiceRoom.acceptInvitation(identifier: identifier, callback: nil)}}
// 发送端:发送文本消息[[TRTCVoiceRoom sharedInstance] sendRoomTextMsg:@"hello world" callback:^(int code, NSString * _Nonnull message) {}];// 接收端:监听文本消息- (void)onRecvRoomTextMsg:(NSString *)messageuserInfo:(VoiceRoomUserInfo *)userInfo {// 收到的message信息处理方法}
// 发送端:发送文本消息self.mTRTCVoiceRoom.sendRoomTextMsg(message: message) { (code, message) in}// 接收端:监听文本消息func onRecvRoomTextMsg(message: String, userInfo: VoiceRoomUserInfo) {//收到的message信息处理方法}
// 例如:发送端:您可以通过自定义Cmd来区分弹幕和点赞消息// eg:"CMD_DANMU"表示弹幕消息,"CMD_LIKE"表示点赞消息[[TRTCVoiceRoom sharedInstance] sendRoomCustomMsg:@"CMD_DANMU" message:@"hello world" callback:^(int code, NSString * _Nonnull message) {}];[[TRTCVoiceRoom sharedInstance] sendRoomCustomMsg:@"CMD_LIKE" message:@"" callback:^(int code, NSString * _Nonnull message) {}];- (void)onRecvRoomCustomMsg:(NSString *)cmdmessage:(NSString *)messageuserInfo:(VoiceRoomUserInfo *)userInfo {if ([cmd isEqualToString:@"CMD_DANMU"]) {// 接收到弹幕消息}if ([cmd isEqualToString:@"CMD_LIKE"]) {// 接收到点赞消息}}
// 例如:发送端:您可以通过自定义Cmd来区分弹幕和点赞消息// eg:"CMD_DANMU"表示弹幕消息,"CMD_LIKE"表示点赞消息self.mTRTCVoiceRoom.sendRoomCustomMsg(cmd: “CMD_DANMU”, message: "hello world", callback: nil)self.mTRTCVoiceRoom.sendRoomCustomMsg(cmd: "CMD_LIKE", message: "", callback: nil)// 接收端:监听自定义消息func onRecvRoomCustomMsg(cmd: String, message: String, userInfo: VoiceRoomUserInfo) {if cmd == "CMD_DANMU" {// 收到弹幕消息}if cmd == "CMD_LIKE" {// 收到点赞消息}}
本页内容是否解决了您的问题?