本文将介绍如何快速完成 Objective-C 版本的 iOS RTC Engine 的接入,实现一个基本的音视频通话。
环境准备
Xcode 13.0+。
iOS 9.0 以上的 iPhone 或者 iPad 真机。
项目已配置有效的开发者签名。
接入指引
步骤1. 导入 SDK
1. 在终端窗口执行以下命令安装 CocoaPods ,如果您已经完成 CocoaPods 的安装,可以跳过该步。
sudo gem install cocoapods
2. 在终端窗口中进入项目根目录,执行以下命令为项目创建 Podfile 文件。
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
执行完成,会生成新的 .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 Description 和 Privacy-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;
2. 在AppDelegate.h
文件中声明 TRTCCloud 属性和 TRTCCloudDelegate 接口。
@interface AppDelegate : UIResponder <UIApplicationDelegate, TRTCCloudDelegate>
@property (nonatomic, strong) TRTCCloud *trtcCloud;
3. 进入AppDelegate.m
文件后,在 didFinishLaunchingWithOptions
方法中调用sharedInstance
创建 TRTC 实例,并设置事件监听。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_trtcCloud = [TRTCCloud sharedInstance];
_trtcCloud.delegate = self;
return YES;
}
- (void)onError:(TXLiteAVError)errCode
errMsg:(nullable NSString *)errMsg
extInfo:(nullable NSDictionary *)extInfo{
NSString *errCodeStr = [NSString stringWithFormat:@"%ld", (long)errCode];
NSString *extInfoStr = extInfo ? [extInfo description] : @"";
NSString *notification = [NSString stringWithFormat:@"%@, %@, %@", errCodeStr, errMsg, extInfoStr];
[self toastTip:notification];
}
- (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 | |
userId | string | 您指定的用户 ID。 |
userSig | string | |
roomId | number | 您指定的房间 ID,通常是唯一的房间 ID。 |
#import "AppDelegate.h" // 导入 "AppDelegate.h" 文件
- (void)enterRoom {
TRTCParams *trtcParams = [[TRTCParams alloc] init];
trtcParams.sdkAppId = 1400000123;
trtcParams.userId = @"denny";
trtcParams.userSig = @"";
trtcParams.roomId = 123321;
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];
}
- (void)onEnterRoom:(NSInteger)result {
}
步骤5. 打开/关闭摄像头
1. 在ViewController.h
文件中声明 UIWindow 和 UIView 属性。
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, strong) UIView *localCameraVideoView;
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];
self.localCameraVideoView = [[UIView alloc] initWithFrame:self.view.bounds];
self.localCameraVideoView.backgroundColor = [UIColor blackColor];
[self viewDidAppear];
}
- (void)viewDidAppear {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[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. 播放/停止视频流
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"];
[appDelegate.trtcCloud stopAllRemoteView];
步骤8. 播放/停止音频流
调用 muteRemoteAudio
选择播放或停止远端声音。
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud muteRemoteAudio:@"denny" mute:YES];
[appDelegate.trtcCloud muteAllRemoteAudio:YES];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud muteRemoteAudio:@"denny" mute:NO];
[appDelegate.trtcCloud muteAllRemoteAudio:NO];
步骤9. 退出房间
调用exitRoom
退出当前的房间,TRTC SDK 会在退房结束后通过 onExitRoom
回调事件通知您。
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.trtcCloud exitRoom];
- (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...");
}
}
常见问题
联系我们
如果有任何需要或者反馈,您可以联系:info_rtc@tencent.com。
本页内容是否解决了您的问题?