本文将介绍如何快速完成腾讯云 TRTC(实时音视频)的接入,实现一个基本的音视频通话。
前提条件
开通服务
1. 登录 实时音视频控制台,单击创建应用。如果您已经完成创建,可以跳过该操作。
2. 在创建应用成功后,您可以在应用管理中获取到您的 SDKAppID 和 SDK 密钥。

SDKAppID: 控制台创建的 TRTC 应用的唯一标识。
UserSig(SDK 密钥):用户鉴权凭证,由您的业务服务器生成。
环境准备
在开始之前,请确保您已满足下列要求:
Xcode 11.0 及以上版本。
确保您的项目已设置有效的开发者签名。
两部 iOS 9.0 及以上真机。
创建项目
创建项目具体步骤
1. 打开 Xcode,创建一个新的 iOS 项目。
2. 点击 Create New Project。
3. 选择 iOS > App,点击 Next。

4. 设置你的项目名称、包名、保存路径等信息。
5. 点击 Finish 创建项目。
集成并引入 SDK
步骤1:导入 SDK
1. 在终端窗口执行以下命令安装 CocoaPods ,如果您已经完成 CocoaPods 的安装,可以跳过该步。
sudo gem install cocoapods
2. 在终端窗口中进入项目根目录,执行以下命令为项目创建 Podfile 文件。
pod init
3. 编辑并保存 Podfile 文件 内容如下。
platform :ios, '8.0'# 将 App 修改为您项目的名称target 'App' dopod 'TXLiteAVSDK_TRTC', :podspec => 'https://liteav.sdk.qcloud.com/pod/liteavsdkspec/TXLiteAVSDK_TRTC.podspec'end
4. 在终端窗口中执行以下命令更新本地库文件,完成对 TRTC SDK 的下载。
pod install
说明:
执行
pod install 完成后,会生成新的 .xcworkspace 工程文件,双击打开进行后续工作。步骤2: 配置工程
1. 打开
.xcworkspace 工程文件后,在 Xcode 的导航栏中点击左侧的 Project Navigator,点击您的项目名称并确保在编辑区域选择正确的 TARGETS。2. 在 Build Settings 选项卡中搜索 User Scripting Sandboxing 其值设置为 No,以允许用户脚本访问更广泛的系统资源和文件。

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

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

实现步骤
步骤1:创建 TRTC 实例
1. 在
AppDelegate.h 文件中引入 TRTC SDK。@import TXLiteAVSDK_TRTC; // 引入 TRTC SDK 模块
2. 在
AppDelegate.h 文件中声明对应的 RTCCloud 属性和 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)errCodeerrMsg:(nullable NSString *)errMsgextInfo:(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:tippreferredStyle:UIAlertControllerStyleAlert];UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Confirm"style:UIAlertActionStyleDefaulthandler: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];});}
步骤2:打开/关闭摄像头
1. 在
ViewController.h 文件中声明 UIWindow 和 UIView 属性。@property (strong, nonatomic) UIWindow *window; // 添加 UIWindow 属性@property (nonatomic, strong) UIView *localCameraVideoView; // 添加 UIView 属性
2. 设置本地预览的渲染参数
setLocalRenderParams ,并调用 startLocalPreview 进行本地预览,成功调用 enterRoom 后开始推流。参数 | 参数说明 | 取值 | 取值说明 |
图像的顺时针旋转角度。支持 90、180 以及 270 旋转角度,默认值:TRTCCloudDef.TRTC_VIDEO_ROTATION_0 | TRTCVideoRotation_0 | 不旋转。 | |
| | TRTCVideoRotation_90 | 顺时针旋转90度。 |
| | TRTCVideoRotation_180 | 顺时针旋转180度。 |
| | TRTCVideoRotation_270 | 顺时针旋转270度。 |
画面填充模式。填充(画面可能会被拉伸裁剪)或适应(画面可能会有黑边),默认值:TRTCCloudDef.TRTC_VIDEO_RENDER_MODE_FILL。 | TRTCVideoFillMode_Fill | 填充模式:即画面内容居中等比缩放以充满整个显示区域,超出显示区域的部分将会被裁剪掉,此模式下画面可能不完整。 | |
| | TRTCVideoFillMode_Fit | 适应模式:即按画面长边进行缩放以适应显示区域,短边部分会被填充为黑色,此模式下图像完整但可能留有黑边。 |
| | TRTCVideoFillMode_ScaleFill | 缩放填充模式:即无论画面的宽高比,都会被拉伸或压缩以完全填充显示区域,此模式下画面宽高比可能会被改变,导致渲染画面变形。 |
画面镜像模式。默认值:TRTCCloudDef.TRTC_VIDEO_MIRROR_TYPE_AUTO。 | TRTCVideoMirrorTypeAuto | 自动模式:如果正使用前置摄像头则开启镜像,如果是后置摄像头则不开启镜像(仅适用于移动设备)。 | |
| | TRTCVideoMirrorTypeEnable | 强制开启镜像,不论当前使用的是前置摄像头还是后置摄像头。 |
| | TRTCVideoMirrorTypeDisable | 强制关闭镜像,不论当前使用的是前置摄像头还是后置摄像头。 |
- (void)startLocalPreview {// 设置本地预览渲染参数TRTCRenderParams *trtcRenderParams = [[TRTCRenderParams alloc] init];trtcRenderParams.rotation = TRTCVideoRotation0;trtcRenderParams.fillMode = TRTCVideoFillMode_Fill;trtcRenderParams.mirrorType = TRTCVideoMirrorTypeAuto;AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];[appDelegate.trtcCloud setLocalRenderParams:trtcRenderParams];// 对采集内容进行本地预览[appDelegate.trtcCloud startLocalPreview:YES view:self.localCameraVideoView];}
3. 调用
stopLocalPreview 关闭摄像头预览并停止推送本地视频信息。AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];[appDelegate.trtcCloud stopLocalPreview];
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.// 初始化 localCameraVideoViewself.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
步骤3:前后摄像头切换
调用
TXDeviceManager#switchCamera(true/false) 进行前后摄像头切换。// 默认开启前置摄像头,切换为后置摄像头AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];TXDeviceManager * deviceManager = [appDelegate.trtcCloud getDeviceManager];if([deviceManager isFrontCamera]) {[deviceManager switchCamera:NO];}// 切换为前置摄像头AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];TXDeviceManager * deviceManager = [appDelegate.trtcCloud getDeviceManager];[deviceManager switchCamera:YES];
步骤4:开启/关闭自动对焦
调用
TXDeviceManager#enableCameraAutoFocus(true/false) 开启和关闭自动对焦。// 若设备支持自动识别人脸位置,开启自动对焦功能AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];TXDeviceManager * deviceManager = [appDelegate.trtcCloud getDeviceManager];if([deviceManager isAutoFocusEnabled]) {[deviceManager enableCameraAutoFocus:YES];}// 关闭自动对焦功能AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];TXDeviceManager * deviceManager = [appDelegate.trtcCloud getDeviceManager];[deviceManager enableCameraAutoFocus:NO];
步骤5:开启/关闭闪光灯
调用
TXDeviceManager#enableCameraTorch(true/false) 开启和关闭闪光灯。// 切换后置摄像头时可开启闪光灯AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];TXDeviceManager * deviceManager = [appDelegate.trtcCloud getDeviceManager];if(![deviceManager isFrontCamera]) {[deviceManager enableCameraTorch:YES];}// 关闭闪光灯AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];TXDeviceManager * deviceManager = [appDelegate.trtcCloud getDeviceManager];[deviceManager enableCameraTorch:NO];
步骤6:打开/关闭麦克风
调用
TRTCCloud#startLocalAudio 开启麦克风采集,请根据您的需求选择以下声音质量参数:参数 | 描述 |
人声模式:单声道;编码码率:18kbps;具备几个模式中最强的网络抗性,适合语音通话为主的场景,例如在线会议,语音通话等。 | |
默认模式:单声道;编码码率:50kbps;介于 Speech 和 Music 之间的档位,SDK 默认档位,推荐选择。 | |
音乐模式:全频带立体声;编码码率:128kbps;适合需要高保真传输音乐的场景,例如在线 K 歌、音乐直播等。 |
// 开启麦克风采集,设置当前场景为:语音模式// 具有高的噪声抑制能力,有强有弱的网络阻力AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];[appDelegate.trtcCloud startLocalAudio:TRTCAudioQualitySpeech];
// 开启麦克风采集,设置当前场景为:音乐模式// 为获得高保真度,低音质损失,建议配合专业声卡使用AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];[appDelegate.trtcCloud startLocalAudio:TRTCAudioQualityMusic];
调用
TRTCCloud#stopLocalAudio 关闭麦克风采集并停止推送本地音频信息。AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];[appDelegate.trtcCloud stopLocalAudio];
步骤7:进入房间
1. 设置进房参数 TRTCParams 并调用
TRTCCloud#enterRoom 即可成功进入房间,该方法通常在点击开始通话按钮后调用。2. 有关更详细的参数描述,请参考接口文档
TRTCCloud#enterRoom。#import "AppDelegate.h" // 导入 "AppDelegate.h" 文件- (void)enterRoom {// 将以下 trtcParams 参数修改为自己的参数TRTCParams *trtcParams = [[TRTCParams alloc] init];trtcParams.sdkAppId = 1400000123;trtcParams.userId = @"denny";trtcParams.userSig = @"your_user_sig";trtcParams.strRoomId = @"123abc";// 对于多人视频通话场景,推荐使用 TRTC_APP_SCENE_LIVEAppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];[appDelegate.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];}// 监听 `onEnterRoom` 事件- (void)onEnterRoom:(NSInteger)result {// 处理 onEnterRoom 事件}
步骤8:播放/停止视频流
1. 在进入房间之前对
TRTCCloudDelegate#onUserVideoAvailable 进行监听,当您收到 TRTCCloudDelegate#onUserVideoAvailable(userId, true) 通知时,表示该路画面已经有可播放的视频帧到达。2. 调用
TRTCCloud#startRemoteView 对远端采集画面进行播放。- (void)startRemoteView{// 播放远端画面AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];[appDelegate.trtcCloud startRemoteView:@"denny" streamType:TRTCVideoStreamTypeBig view:self.remoteVideoView];}
3. 调用
TRTCCloud#stopRemoteView 来停止播放远端画面。AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];// 仅停止播放 denny 画面[appDelegate.trtcCloud stopRemoteView:@"denny"];// 停止播放所有视频[appDelegate.trtcCloud stopAllRemoteView];
步骤9:播放/停止音频流
1. 默认情况下,SDK 将自动播放远程音频,因此您不需要调用任何 API 来手动播放它。
2. 但当您不喜欢自动播放音频时,可以调用
TRTCCloud#muteRemoteAudio 选择播放或停止远端声音。AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];// 仅静音 denny[appDelegate.trtcCloud muteRemoteAudio:@"denny" mute:YES];// 静音所有远端用户[appDelegate.trtcCloud muteAllRemoteAudio:YES];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];// 仅取消静音 denny[appDelegate.trtcCloud muteRemoteAudio:@"denny" mute:NO];// 取消静音所有远端用户[appDelegate.trtcCloud muteAllRemoteAudio:NO];
步骤10:退出房间
调用
TRTCCloud#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...");}}
编译并运行
1. 将 iOS 设备连接至电脑,在设备上选择设置 > 隐私与安全性 > 开发者模式,打开开发者模式。
2. 在 Xcode 界面顶部的 iOS Device 选项中选择你用来测试的 iOS 设备。


3. 在项目 TARGETS 的 Signing & Capabilities 界面勾选 Automatically manage signing,然后在每一个 Target 下配置你的苹果开发者账号和 Bundle Identifier。如果尚未在 Xcode 中登录 Apple ID,请先在 Xcode > Preferences > Accounts 中添加你的开发者账号或 Apple ID。


说明:
本 Demo 默认集成了音视频通话功能,由于该功能依赖的音视频 SDK 暂不支持模拟器,因此建议使用真机调试、运行 Demo。
4. 运行 App。


常见问题
您可以在 API 概览 查看所有函数列表及其描述。
如果您的接入和使用中遇到问题,请参见 常见问题。
下一步
至此,你已成功实现基本的音视频通话,你还可以参考下列文档了解更多核心功能:
调整视频画质
调整画面方向