tencent cloud

文档反馈

最后更新时间:2024-09-24 18:15:50
    本文将介绍如何快速完成 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. General选项卡中 Frameworks, Libraries, and Embedded Content 部分添加 TXLiteAVSDK_TRTC.xcframework,确保在 Embed 中选择 Embed & Sign
    
    
    
    3. Build Settings选项卡中搜索 User Script Sandboxing,将其值置为 No,以允许用户脚本访问更广泛的系统资源和文件。
    
    
    
    4. Info.plist选项卡中添加 Privacy-Microphone Usage DescriptionPrivacy-Microphone Usage Description ,并填入 Microphone/Camera 使用的目标提示语,获取麦克风和摄像头的使用权限。
    
    
    
    5. 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. 进入房间

    1. Tencent RTC 控制台 中单击Create Application,创建成功后在 Application Overview 中获取 SDKAppID
    
    
    
    2. UserSig Tools 中下拉选择 SDKAppID ,并输入您自己的用户名(UserID),单击Generate即可获取您自己的 UserSig
    
    
    
    3. 设置进房参数 TRTCParams 并调用 enterRoom 即可成功进入房间。
    主播角色:
    #import "AppDelegate.h" // 导入 "AppDelegate.h" 文件
    
    - (void)enterRoom {
    // 将以下 trtcParams 参数修改为自己的参数
    TRTCParams *trtcParams = [[TRTCParams alloc] init];
    trtcParams.sdkAppId = 1400000123;
    trtcParams.roomId = 123321;
    trtcParams.userId = @"anchor";
    trtcParams.userSig = @"";
    trtcParams.role = TRTCRoleAnchor; // 以主播角色进入房间
    // 对于多人视频通话场景,推荐使用 TRTC_APP_SCENE_LIVE
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];
    }
    
    // 监听 `onEnterRoom` 事件
    - (void)onEnterRoom:(NSInteger)result {
    // 处理 onEnterRoom 事件
    }
    观众角色:
    #import "AppDelegate.h" // 导入 "AppDelegate.h" 文件
    
    - (void)enterRoom {
    // 将以下 trtcParams 参数修改为自己的参数
    TRTCParams *trtcParams = [[TRTCParams alloc] init];
    trtcParams.sdkAppId = 1400000123;
    trtcParams.roomId = 123321;
    trtcParams.userId = @"audience";
    trtcParams.userSig = @"";
    trtcParams.role = TRTCRoleAudience; // 以观众角色进入房间
    // 对于多人视频通话场景,推荐使用 TRTC_APP_SCENE_LIVE
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];
    }
    
    // 监听 `onEnterRoom` 事件
    - (void)onEnterRoom:(NSInteger)result {
    // 处理 onEnterRoom 事件
    }
    注意:
    如果以观众角色进入房间,sdkAppIdroomId 需要与主播端参数相同,将 userIduserSig 修改为观众的参数即可。

    步骤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];
    }
    // 切换为前置摄像头
    [deviceManager switchCamera:true];
    
    // 若设备支持自动识别人脸位置,开启自动对焦功能
    if([deviceManager isAutoFocusEnabled]) {
    [deviceManager enableCameraAutoFocus:true];
    }
    // 关闭自动对焦功能
    [deviceManager enableCameraAutoFocus:false];
    
    // 切换后置摄像头时可开启闪光灯
    if(![deviceManager isFrontCamera]) {
    [deviceManager enableCameraTorch:true];
    }
    // 关闭闪光灯
    [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. ViewController.h文件中声明 UIWindowUIView 属性。
    @property (strong, nonatomic) UIWindow *window; // 添加 UIWindow 属性
    @property (nonatomic, strong) UIView *remoteCameraVideoView; // 添加 UIView 属性
    2. ViewController.m中初始化 remoteCameraVideoView
    #import "AppDelegate.h" // 引入 "AppDelegate.h" 文件
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 初始化 localCameraVideoView
    self.remoteCameraVideoView = [[UIView alloc] initWithFrame:self.view.bounds];
    self.remoteCameraVideoView.backgroundColor = [UIColor blackColor];
    [self viewDidAppear];
    }
    
    - (void)viewDidAppear {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    // 将localCameraVideoView添加到当前视图的视图层次结构中
    [self.view addSubview:self.remoteCameraVideoView];
    }
    @end
    3. 观众端设置本地预览的渲染参数 setLocalRenderParams ,并调用startRemoteView进行本地预览,成功调用enterRoom后开始推流。
    - (void)startRemoteView {
    // 设置本地预览渲染参数
    TRTCRenderParams *trtcRenderParams = [[TRTCRenderParams alloc] init];
    trtcRenderParams.fillMode = TRTCVideoFillMode_Fill;
    trtcRenderParams.mirrorType = TRTCVideoMirrorTypeAuto;
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.trtcCloud setLocalRenderParams:trtcRenderParams];
    
    // 播放主播端画面
    [appDelegate.trtcCloud startRemoteView:@"anchor" streamType:TRTCVideoStreamTypeBig view:self.remoteCameraVideoView];
    }
    调用stopRemoteView 停止播放主播端画面。
    // 停止播放
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.trtcCloud stopRemoteView:@"anchor"]; // 停止播放远端画面
    [appDelegate.trtcCloud stopAllRemoteView]; // 停止播放所有视频

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

    观众端可以调用 muteRemoteAudio 选择播放或停止主播端声音。
    // 静音
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.trtcCloud muteRemoteAudio:@"anchor" mute:YES];// 静音 anchor
    [appDelegate.trtcCloud muteAllRemoteAudio:YES]; // 静音所有远端用户
    
    // 取消静音
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.trtcCloud muteRemoteAudio:@"anchor" mute:NO]; // 取消静音 anchor
    [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。
    联系我们

    联系我们,为您的业务提供专属服务。

    技术支持

    如果你想寻求进一步的帮助,通过工单与我们进行联络。我们提供7x24的工单服务。

    7x24 电话支持