本文将介绍如何快速完成 Mac RTC Engine 的接入,实现一个基本的音视频通话。
环境准备
Xcode 9.0+。
OS X10.10+ 的 Mac 真机。
项目已配置有效的开发者签名。
步骤1. 导入 SDK
1. 在终端窗口输入以下命令安装 CocoaPods ,如果您已经完成 CocoaPods 的安装,可以跳过该步。
sudo gem install cocoapods
2. 在终端窗口中进入 TRTCDemo 所在的路径,输入以下命令为您的项目创建 Podfile 文件。
3. 编辑 Podfile 文件为如下内容,将 Your Target 修改为您自己的项目名称。
platform :osx, '10.10'
target 'Your Target' do
pod 'TXLiteAVSDK_TRTC_Mac', :podspec => 'https://liteav.sdk.qcloud.com/pod/liteavsdkspec/TXLiteAVSDK_TRTC_Mac.podspec'
end
4. 在终端窗口中输入以下命令更新本地库文件,并安装 SDK 。
说明:
pod 命令执行完后,会生成集成了 SDK 的 .xcworkspace 后缀的工程文件,双击打开.xcworkspace 文件进行后续工作。
步骤2. 配置项目
1. 打开.xcworkspace
文件后,在General
中的 Frameworks, Libraries, and Embedded Content 部分添加 TXLiteAVSDK_TRTC.xcframework 和 ScreenCaptureKit.framework 。
2. 在Build Settings
中搜索 User Script Sandboxing,将其值置为 No
。
3. 在 Info.plist
中添加 Privacy-Microphone Usage Description 和 Privacy-Microphone Usage Description ,并填入 Microphone/Camera 使用的目标提示语,获取麦克风和摄像头的使用权限。
4. 在 Signing & Capabilities
中 App Sandbox 部分勾选以下内容。
步骤3. 创建 TRTC 实例
1. 在 AppDelegate.h
文件中添加对 SDK 的模块引用:
@import TXLiteAVSDK_TRTC_Mac;
2. 在AppDelegate.h
文件中添加以下属性,并声明 toastTip:
方法。
#import <Cocoa/Cocoa.h>
@import TXLiteAVSDK_TRTC_Mac;
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (nonatomic, strong) NSWindow *window;
@property (nonatomic, strong) TRTCCloud *trtcCloud;
@property (nonatomic, strong) NSView *localCameraVideoView;
- (void)toastTip:(NSString *)tip;
@end
3. 在AppDelegate.m
文件中实现 toastTip:
方法。
- (void)toastTip:(NSString *)tip {
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:tip];
[alert runModal];
}
4. 在 applicationDidFinishLaunching()
方法中调用初始化接口创建 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{
if (ERR_CAMERA_NOT_AUTHORIZED == errCode) {
NSString *errorInfo = @"Current application is not authorized to use the camera:";
errorInfo = [errorInfo stringByAppendingString : errMsg];
[self toastTip:errorInfo];
}
}
步骤4. 进入房间
1. 在Tencent RTC 控制台中单击Create Application
,在 Application Overview 中获取 SDKAppID 。 2. 在UserSig Tools中下来选择 SDKAppID ,并输入您自己的用户名(UserID),单击Generate
即可获取您自己的 UserSig 。 3. 在 didFinishLaunchingWithOptions()
方法中设置进房参数 TRTCParams 后,调用 enterRoom
接口函数即可进入房间。
主播角色:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
TRTCParams *trtcParams = [[TRTCParams alloc] init];
trtcParams.sdkAppId = 1400000123;
trtcParams.roomId = 123321;
trtcParams.userId = @"denny";
trtcParams.userSig = @"";
trtcParams.role = TRTCRoleAnchor;
[self.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];
return YES;
}
- (void)onEnterRoom:(NSInteger)result {
if (result > 0) {
[self toastTip:@"Enter room succeed!"];
} else {
[self toastTip:@"Enter room failed!"];
}
}
观众角色:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
TRTCParams *trtcParams = [[TRTCParams alloc] init];
trtcParams.sdkAppId = 1400000123;
trtcParams.roomId = 123321;
trtcParams.userId = @"denny";
trtcParams.userSig = @"";
trtcParams.role = TRTCRoleAudience;
[self.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];
return YES;
}
- (void)onEnterRoom:(NSInteger)result {
if (result > 0) {
[self toastTip:@"Enter room succeed!"];
} else {
[self toastTip:@"Enter room failed!"];
}
}
注意:
如果以观众角色进入房间,sdkAppId 和 roomId 需要与主播端相同,而userId 和 userSig 需要替换为自己的值。
步骤5. 打开摄像头
在 didFinishLaunchingWithOptions()
方法中初始化 localCameraVideoView ,并调用 setLocalRenderParams
接口来设置本地预览的渲染参数。
self.window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600) styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:NO];
[self.window center];
[self.window setTitle:@"TRTCDemo_Mac"];
[self.window makeKeyAndOrderFront:nil];
self.window.releasedWhenClosed = NO;
self.localCameraVideoView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300)];
[self.window.contentView addSubview:self.localCameraVideoView];
self.localCameraVideoView.frame = self.window.contentView.bounds;
TRTCRenderParams *trtcRenderParams = [[TRTCRenderParams alloc] init];
trtcRenderParams.fillMode = TRTCVideoFillMode_Fill;
trtcRenderParams.mirrorType = TRTCVideoMirrorTypeAuto;
[self.trtcCloud setLocalRenderParams:trtcRenderParams];
[_trtcCloud startLocalPreview:_localCameraVideoView];
步骤6. 打开麦克风
您可以调用 startLocalAudio
来开启麦克风采集,该接口需要您通过 quality
参数确定采集模式,建议根据您的需求选择以下其中一个适合您项目的模式。
[self.trtcCloud startLocalAudio:TRTCAudioQualitySpeech];
[self.trtcCloud startLocalAudio:TRTCAudioQualityMusic];
步骤7. 播放/停止视频流
按照步骤1-4以观众角色进入 denny 的房间后,您可以通过调用接口 startRemoteView
来播放远端用户的视频画面。
[self.trtcCloud startRemoteView:@"denny" streamType:TRTCVideoStreamTypeBig view:cameraView];
之后,您通过调用 stopRemoteView
接口停止某个远端用户的视频,也可以通过接口 stopAllRemoteView
停止播放所有远端用户的视频。
[self.trtcCloud stopRemoteView:@"denny" streamType:TRTCVideoStreamTypeBig view:cameraView];
[self.trtcCloud stopAllRemoteView];
步骤8. 播放/停止音频流
您可以通过调用接口 muteRemoteAudio("denny",true)
来静音远端用户 denny 的声音。
[self.trtcCloud muteRemoteAudio:@"denny" mute:YES];
之后也可以通过调用接口 muteRemoteAudio("denny",false)
来解除对他的静音。
[self.trtcCloud muteRemoteAudio:@"denny" mute:YES];
步骤9. 退出房间
您可以调用接口 exitRoom
即可退出当前的房间,SDK 会在退房结束后通过 onExitRoom(int reason)
回调事件通知您。
[self.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。
本页内容是否解决了您的问题?