tencent cloud

实时音视频

动态与公告
产品动态
产品近期公告
关于 TRTC Live 正式上线的公告
关于TRTC Conference 正式版上线的公告
Conference 商业化版本即将推出
关于多人音视频 Conference 开启内测公告
关于音视频通话 Call 正式版上线的公告
关于腾讯云音视频终端 SDK 播放升级及新增授权校验的公告
关于 TRTC 应用订阅套餐服务上线的相关说明
产品简介
产品概述
基本概念
产品功能
产品优势
应用场景
性能数据
购买指南
计费概述
免费时长说明
月订阅
现收现付
TRTC 逾期与暂停政策
常见问题解答
退款说明
新手指引
Demo 体验
视频通话 SDK
组件介绍
开通服务
跑通 Demo
快速接入
离线唤醒
会话聊天
云端录制
AI 降噪
界面定制
Chat 集成通话能力
更多特性
无 UI 集成
服务端 API
客户端 API
解决方案
错误码表
发布日志
常见问题
视频会议 SDK
组件介绍(TUIRoomKit)
开通服务(TUIRoomKit)
跑通 Demo(TUIRoomKit)
快速接入(TUIRoomKit)
屏幕共享(TUIRoomKit)
预定会议(TUIRoomKit)
会中呼叫(TUIRoomKit)
界面定制(TUIRoomKit)
虚拟背景(TUIRoomKit)
会议控制(TUIRoomKit)
云端录制(TUIRoomKit)
AI 降噪(TUIRoomKit)
会中聊天(TUIRoomKit)
机器人推流(TUIRoomKit)
更多特性(TUIRoomKit)
客户端 API(TUIRoomKit)
服务端 API(TUIRoomKit)
常见问题(TUIRoomKit)
错误码 (TUIRoomKit)
SDK更新日志(TUIRoomKit)
直播与语聊 SDK
Live 视频直播计费说明
组件介绍
开通服务(TUILiveKit)
跑通 Demo
无 UI 集成
UI 自定义
直播监播
视频直播
语聊房
高级功能
客户端 API
服务端 API
错误码
发布日志
常见问题
RTC Engine
开通服务
SDK 下载
API-Example
接入指引
API-参考手册
高级功能
AI 集成
概述
MCP 配置
Skills 配置
集成指南
常见问题
RTC RESTFUL API
History
Introduction
API Category
Room Management APIs
Stream mixing and relay APIs
On-cloud recording APIs
Data Monitoring APIs
Pull stream Relay Related interface
Web Record APIs
AI Service APIs
Cloud Slicing APIs
Cloud Moderation APIs
Making API Requests
Call Quality Monitoring APIs
Usage Statistics APIs
Data Types
Appendix
Error Codes
控制台指南
应用管理
套餐包管理
用量统计
监控仪表盘
开发辅助
解决方案
实时合唱
常见问题
迁移指南
计费相关
功能相关
UserSig 相关
应对防火墙限制相关
缩减安装包体积相关
Andriod 与 iOS 相关
Web 端相关
Flutter 相关
Electron 相关
TRTCCalling Web 相关
音视频质量相关
其他问题
旧版文档
RTC RoomEngine SDK(旧)
集成 TUIRoom (Web)
集成 TUIRoom (Android)
集成 TUIRoom (iOS)
集成 TUIRoom (Flutter)
集成 TUIRoom (Electron)
TUIRoom API 查询
实现云端录制与回放(旧)
监控仪表盘计费(旧)
协议与策略
安全合规认证
安全白皮书
信息安全说明
服务等级协议
苹果隐私策略:PrivacyInfo.xcprivacy
TRTC 政策
隐私协议
数据处理和安全协议
词汇表

iOS

PDF
聚焦模式
字号
最后更新时间: 2025-06-23 11:17:03
本文将介绍如何快速完成 Objective-C 版本的 iOS RTC Engine 的接入,实现一个基本的音视频通话。

环境准备

Xcode 13.0+。
iOS 9.0 以上的 iPhone 或者 iPad 真机。
项目已配置有效的开发者签名。

接入指引

步骤1. 导入 SDK

1. 在终端窗口执行以下命令安装 CocoaPods ,如果您已经完成 CocoaPods 的安装,可以跳过该步。
sudo gem install cocoapods
2. 在终端窗口中进入项目根目录,执行以下命令为项目创建 Podfile 文件。
pod init
3. 编辑并保存 Podfile 内容如下。
platform :ios, '8.0'

# 将 App 修改为您项目的名称
target 'App' do
pod 'TXLiteAVSDK_TRTC', :podspec => 'https://liteav.sdk.qcloud.com/pod/liteavsdkspec/TXLiteAVSDK_TRTC.podspec'
end
4. 在终端窗口中执行以下命令更新本地库文件,完成对 TRTC SDK 的下载。
pod install
说明:
pod install执行完成,会生成新的 .xcworkspace 工程文件,双击打开.xcworkspace 文件进行后续工作。

步骤2. 配置项目

1. 打开.xcworkspace工程文件后,在 Xcode 的导航栏中点击左侧的 Project Navigator,点击您的项目名称并确保在编辑区域选择正确的 TARGETS
2. Build Settings选项卡中搜索 User Script Sandboxing,将其值置为 No,以允许用户脚本访问更广泛的系统资源和文件。



3. Info.plist选项卡中添加 Privacy-Microphone Usage DescriptionPrivacy-Microphone Usage Description ,并填入 Microphone/Camera 使用的目标提示语,获取麦克风和摄像头的使用权限。



4. Signing & Capabilities选项卡中添加 Background Modes,并勾选 Audio,AirPlay and Picture in Picture,以允许应用在后台运行音频、AirPlay和画中画功能。




步骤3. 创建 TRTC 实例

1. AppDelegate.h 文件中引入 TRTC SDK。
@import TXLiteAVSDK_TRTC; // 引入 TRTC SDK 模块
2. AppDelegate.h文件中声明 TRTCCloud 属性和 TRTCCloudDelegate 接口。
@interface AppDelegate : UIResponder <UIApplicationDelegate, TRTCCloudDelegate> // 添加 TRTCCloudDelegate 接口声明

@property (nonatomic, strong) TRTCCloud *trtcCloud; // 添加 TRTCCloud 属性
3. 进入AppDelegate.m文件后,在 didFinishLaunchingWithOptions 方法中调用sharedInstance创建 TRTC 实例,并设置事件监听。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 创建 TRTC 实例并设置监听
_trtcCloud = [TRTCCloud sharedInstance];
_trtcCloud.delegate = self;
return YES;
}

// 监听并处理 `onError` 事件
- (void)onError:(TXLiteAVError)errCode
errMsg:(nullable NSString *)errMsg
extInfo:(nullable NSDictionary *)extInfo{
// 处理 `Error` 事件,建议对 OnError 的信息通过 'toastTip:' 进行提示
NSString *errCodeStr = [NSString stringWithFormat:@"%ld", (long)errCode];
NSString *extInfoStr = extInfo ? [extInfo description] : @"";
NSString *notification = [NSString stringWithFormat:@"%@, %@, %@", errCodeStr, errMsg, extInfoStr];
[self toastTip:notification];
}

// 实现 'toastTip:'
- (void)toastTip:(NSString *)tip {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Tip"
message:tip
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Confirm"
style:UIAlertActionStyleDefault
handler:nil];
[alert addAction:okAction];
[self.window.rootViewController presentViewController:alert animated:YES completion:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[alert dismissViewControllerAnimated:YES completion:nil];
});
}

步骤4. 进入房间

设置进房参数 TRTCParams 并调用 enterRoom 即可成功进入房间,该方法通常在点击开始通话按钮后调用。
参数
类型
描述
sdkAppId
number
您在 TRTC 控制台中创建的音频和视频应用程序的sdkAppId。
userId
string
您指定的用户 ID。
userSig
string
用户签名,请参见 UserSig
roomId
number
您指定的房间 ID,通常是唯一的房间 ID。
有关更详细的参数描述,请参考接口文档 enterRoom
#import "AppDelegate.h" // 导入 "AppDelegate.h" 文件

- (void)enterRoom {
// 将以下 trtcParams 参数修改为自己的参数
TRTCParams *trtcParams = [[TRTCParams alloc] init];
trtcParams.sdkAppId = 1400000123;
trtcParams.userId = @"denny";
trtcParams.userSig = @"";
trtcParams.roomId = 123321;
// 对于多人视频通话场景,推荐使用 TRTC_APP_SCENE_LIVE
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];
}

// 监听 `onEnterRoom` 事件
- (void)onEnterRoom:(NSInteger)result {
// 处理 onEnterRoom 事件
}

步骤5. 打开/关闭摄像头

1. ViewController.h文件中声明 UIWindowUIView 属性。
@property (strong, nonatomic) UIWindow *window; // 添加 UIWindow 属性
@property (nonatomic, strong) UIView *localCameraVideoView; // 添加 UIView 属性
2. 设置本地预览的渲染参数 setLocalRenderParams ,并调用startLocalPreview进行本地预览,成功调用enterRoom后开始推流。
- (void)startLocalPreview {
// 设置本地预览渲染参数
TRTCRenderParams *trtcRenderParams = [[TRTCRenderParams alloc] init];
trtcRenderParams.fillMode = TRTCVideoFillMode_Fill;
trtcRenderParams.mirrorType = TRTCVideoMirrorTypeAuto;
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud setLocalRenderParams:trtcRenderParams];

// 对采集内容进行本地预览
[appDelegate.trtcCloud startLocalPreview:YES view:self.localCameraVideoView];
}
调用stopLocalPreview关闭摄像头预览并停止推送本地视频信息。
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud stopLocalPreview];
3. 添加“切换前后摄像头”“设置对焦模式”“闪光灯”等设备扩展功能的使用。
// 默认开启前置摄像头,切换为后置摄像头
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
TXDeviceManager * deviceManager = [appDelegate.trtcCloud getDeviceManager];
if([deviceManager isFrontCamera]) {
[deviceManager switchCamera:false];
}

// 切换为前置摄像头
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
TXDeviceManager * deviceManager = [appDelegate.trtcCloud getDeviceManager];
[deviceManager switchCamera:true];
// 若设备支持自动识别人脸位置,开启自动对焦功能
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
TXDeviceManager * deviceManager = [appDelegate.trtcCloud getDeviceManager];
if([deviceManager isAutoFocusEnabled]) {
[deviceManager enableCameraAutoFocus:true];
}

// 关闭自动对焦功能
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
TXDeviceManager * deviceManager = [appDelegate.trtcCloud getDeviceManager];
[deviceManager enableCameraAutoFocus:false];
// 切换后置摄像头时可开启闪光灯
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
TXDeviceManager * deviceManager = [appDelegate.trtcCloud getDeviceManager];
if(![deviceManager isFrontCamera]) {
[deviceManager enableCameraTorch:true];
}

// 关闭闪光灯
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
TXDeviceManager * deviceManager = [appDelegate.trtcCloud getDeviceManager];
[deviceManager enableCameraTorch:false];
4. ViewController.m中初始化 localCameraVideoView ,并将其添加到当前视图的视图层次结构中。
#import "AppDelegate.h" // 引入 "AppDelegate.h" 文件

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

// 初始化 localCameraVideoView
self.localCameraVideoView = [[UIView alloc] initWithFrame:self.view.bounds];
self.localCameraVideoView.backgroundColor = [UIColor blackColor];
[self viewDidAppear];
}

- (void)viewDidAppear {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// 将 localCameraVideoView 添加到当前视图的视图层次结构中
[self.view addSubview:self.localCameraVideoView];

// 开启本地预览
[appDelegate.trtcCloud startLocalPreview:YES view:self.localCameraVideoView];
}
@end

步骤6. 打开/关闭麦克风

调用 startLocalAudio 开启麦克风采集,请根据您的需求选择以下其中一个声音质量参数Quality
// 开启麦克风采集,设置当前场景为:语音模式
// 具有高的噪声抑制能力,有强有弱的网络阻力
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud startLocalAudio:TRTCAudioQualitySpeech];
// 开启麦克风采集,设置当前场景为:音乐模式
// 为获得高保真度,低音质损失,建议配合专业声卡使用
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud startLocalAudio:TRTCAudioQualityMusic];
调用stopLocalAudio关闭麦克风采集并停止推送本地音频信息。
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud stopLocalAudio];

步骤7. 播放/停止视频流

1. 在进入房间之前对 onUserVideoAvailable 进行监听,当您收到onUserVideoAvailable(userId, YES)通知时,表示该路画面已经有可播放的视频帧到达。
2. 调用startRemoteView进行本地预览,成功调用enterRoom后开始推流。
- (void)startRemoteView {
// 播放远端画面
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud startRemoteView:@"denny" streamType:TRTCVideoStreamTypeBig view:self.localCameraVideoView];
}
调用stopRemoteView 停止播放远端画面。
// 停止播放
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud stopRemoteView:@"denny"]; // 仅停止播放 denny 画面
[appDelegate.trtcCloud stopAllRemoteView]; // 停止播放所有视频

步骤8. 播放/停止音频流

调用 muteRemoteAudio 选择播放或停止远端声音。
// 静音
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud muteRemoteAudio:@"denny" mute:YES]; // 仅静音 denny

[appDelegate.trtcCloud muteAllRemoteAudio:YES]; // 静音所有远端用户
// 取消静音
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud muteRemoteAudio:@"denny" mute:NO]; // 仅取消静音 denny

[appDelegate.trtcCloud muteAllRemoteAudio:NO]; // 取消静音所有远端用户

步骤9. 退出房间

调用exitRoom退出当前的房间,TRTC SDK 会在退房结束后通过 onExitRoom 回调事件通知您。
// 退出当前房间
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud exitRoom];

// 监听 `onExitRoom` 回调
- (void)onExitRoom:(NSInteger)reason {
if (reason == 0) {
NSLog(@"Exit current room by calling the 'exitRoom' api of sdk ...");
} else if (reason == 1) {
NSLog(@"Kicked out of the current room by server through the restful api...");
} else if (reason == 2) {
NSLog(@"Current room is dissolved by server through the restful api...");
}
}

常见问题

您可以在 API 参考 查看所有函数列表及其描述。
如果您的接入和使用中遇到问题,请参见 常见问题

联系我们

如果有任何需要或者反馈,您可以联系:info_rtc@tencent.com。

帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈