tencent cloud

文档反馈

最后更新时间:2024-07-18 15:20:46
    本文将介绍如何快速完成 Mac RTC Engine 的接入,实现一个基本的音视频通话。

    环境准备

    Xcode 9.0+。
    OS X10.10+ 的 Mac 真机。
    项目已配置有效的开发者签名。

    步骤1. 导入 SDK

    1. 在终端窗口输入以下命令安装 CocoaPods ,如果您已经完成 CocoaPods 的安装,可以跳过该步。
    sudo gem install cocoapods
    2. 在终端窗口中进入 TRTCDemo 所在的路径,输入以下命令为您的项目创建 Podfile 文件。
    pod init
    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 install
    说明:
    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 DescriptionPrivacy-Microphone Usage Description ,并填入 Microphone/Camera 使用的目标提示语,获取麦克风和摄像头的使用权限。
    
    
    
    4. Signing & CapabilitiesApp 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; // Add window property
    @property (nonatomic, strong) TRTCCloud *trtcCloud; // Add trtcCloud property
    @property (nonatomic, strong) NSView *localCameraVideoView; // Add localCameraVideoView property
    
    - (void)toastTip:(NSString *)tip; // Declare the toastTip: method
    
    @end
    3. AppDelegate.m 文件中实现 toastTip: 方法。
    // Implement toastTip: method
    - (void)toastTip:(NSString *)tip {
    NSAlert *alert = [[NSAlert alloc] init];
    [alert setMessageText:tip];
    [alert runModal];
    }
    4. applicationDidFinishLaunching() 方法中调用初始化接口创建 TRTC 实例,并设置事件回调接口。
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // Create trtc instance(singleton) and set up event listeners
    _trtcCloud = [TRTCCloud sharedInstance];
    _trtcCloud.delegate = self;
    
    return YES;
    }
    
    // Listen to the "onError" event, and print logs for errors such as "Camera is not authorized"
    - (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 {
    // Override point for customization after application launch.
    // ...Other codes
    // Please replace each field in TRTCParams with your own parameters
    TRTCParams *trtcParams = [[TRTCParams alloc] init];
    trtcParams.sdkAppId = 1400000123; // Please replace with your own SDKAppID
    trtcParams.roomId = 123321; // Please replace with your own room number
    trtcParams.userId = @"denny"; // Please replace with your own userid
    trtcParams.userSig = @""; // Please replace with your own userSig
    trtcParams.role = TRTCRoleAnchor;
    // If your application scenario is a video call between several people, please use "TRTC_APP_SCENE_LIVE"
    [self.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];
    return YES;
    }
    
    // Listen for the `onEnterRoom` event of the SDK and learn whether the room is successfully entered
    - (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 {
    // Override point for customization after application launch.
    // ...Other codes
    // Please replace each field in TRTCParams with your own parameters
    TRTCParams *trtcParams = [[TRTCParams alloc] init];
    trtcParams.sdkAppId = 1400000123; // Please replace with your own SDKAppID
    trtcParams.roomId = 123321; // Please replace with your own room number
    trtcParams.userId = @"denny"; // Please replace with your own userid
    trtcParams.userSig = @""; // Please replace with your own userSig
    trtcParams.role = TRTCRoleAudience;
    // If your application scenario is a video call between several people, please use "TRTC_APP_SCENE_LIVE"
    [self.trtcCloud enterRoom:trtcParams appScene:TRTCAppSceneLIVE];
    return YES;
    }
    
    // Listen for the `onEnterRoom` event of the SDK and learn whether the room is successfully entered
    - (void)onEnterRoom:(NSInteger)result {
    if (result > 0) {
    [self toastTip:@"Enter room succeed!"];
    } else {
    [self toastTip:@"Enter room failed!"];
    }
    }
    注意:
    如果以观众角色进入房间,sdkAppIdroomId 需要与主播端相同,而userIduserSig 需要替换为自己的值。

    步骤5. 打开摄像头

    didFinishLaunchingWithOptions() 方法中初始化 localCameraVideoView ,并调用 setLocalRenderParams 接口来设置本地预览的渲染参数。
    // Create a window
    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;
    
    // Initialize localCameraVideoView
    self.localCameraVideoView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300)];
    [self.window.contentView addSubview:self.localCameraVideoView];
    
    // Adjust the localCameraVideoView frame to match the size of the window
    self.localCameraVideoView.frame = self.window.contentView.bounds;
    
    // Set the preview mode of the local screen
    TRTCRenderParams *trtcRenderParams = [[TRTCRenderParams alloc] init];
    trtcRenderParams.fillMode = TRTCVideoFillMode_Fill;
    trtcRenderParams.mirrorType = TRTCVideoMirrorTypeAuto;
    [self.trtcCloud setLocalRenderParams:trtcRenderParams];
    
    // Start a preview of the local camera
    [_trtcCloud startLocalPreview:_localCameraVideoView];

    步骤6. 打开麦克风

    您可以调用 startLocalAudio 来开启麦克风采集,该接口需要您通过 quality 参数确定采集模式,建议根据您的需求选择以下其中一个适合您项目的模式
    // Enable microphone acquisition and set the current scene to: Voice mode
    // For high noise suppression capability, strong and weak network resistance
    [self.trtcCloud startLocalAudio:TRTCAudioQualitySpeech];
    
    // Enable microphone acquisition, and set the current scene to: Music mode
    // For high fidelity acquisition, low sound quality loss, recommended to use with professional sound cards
    [self.trtcCloud startLocalAudio:TRTCAudioQualityMusic];

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

    按照步骤1-4观众角色进入 denny 的房间后,您可以通过调用接口 startRemoteView 来播放远端用户的视频画面。
    // Play denny's camera footage
    [self.trtcCloud startRemoteView:@"denny" streamType:TRTCVideoStreamTypeBig view:cameraView];
    之后,您通过调用 stopRemoteView 接口停止某个远端用户的视频,也可以通过接口 stopAllRemoteView 停止播放所有远端用户的视频。
    // Stop denny's camera footage
    [self.trtcCloud stopRemoteView:@"denny" streamType:TRTCVideoStreamTypeBig view:cameraView];
    // Stop all camera footages
    [self.trtcCloud stopAllRemoteView];

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

    您可以通过调用接口 muteRemoteAudio("denny",true) 来静音远端用户 denny 的声音。
    // Mute user with id denny
    [self.trtcCloud muteRemoteAudio:@"denny" mute:YES];
    之后也可以通过调用接口 muteRemoteAudio("denny",false) 来解除对他的静音。
    // Unmute user with id denny
    [self.trtcCloud muteRemoteAudio:@"denny" mute:YES];

    步骤9. 退出房间

    您可以调用接口 exitRoom 即可退出当前的房间,SDK 会在退房结束后通过 onExitRoom(int reason) 回调事件通知您。
    // Exit current room
    [self.trtcCloud exitRoom];
    
    // Listen for the onExitRoom callback to find out why you checked out
    - (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 电话支持