实时音视频 SparkRTC-SDK使用

时间:2025-02-12 14:55:01

SDK使用

  1. 创建引擎并初始化。

    域名 不需要设置,由SDK自动获取。appId获取方法请参见创建应用
     1 2 3 4 5 6 7 8 9101112
    _rtcEngine = [HWRtcEngine sharedEngine];HWRtcEngineConfig * cfg = [[HWRtcEngineConfig alloc]init];cfg.appId = appid;// appId需在控制台中创建应用后获取cfg.domain = domain;// 该字段已废弃,不需要再传值cfg.countryCode = rtcCountryCode;// 可以根据Grs国家码对照表传值,建议传"CN"cfg.enableHaTrace = YES;cfg.logLevel = HWRtcLogLevelDebug;//输出DEBUG级别日志cfg.logPath = [NSString stringWithFormat:@"%@/",logFilePath];//日志存储路径cfg.enableLog = YES;//开启日志cfg.logSize = 10*1024;cfg.muteAudioRoute = NO;//远端音频路由[_rtcEngine initWithConfig:cfg];

  2. 设置本地窗口。

    12345
    HWRtcVideoCanvas *canvas = [[HWRtcVideoCanvas alloc] init];    canvas.view = [[UIView alloc] initWithFrame:CGRectMacke(0, 0, 90, 160)];//iOS//canvas.view = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 90, 160)];//macOScanvas.uid  = @"HW";    [_rtcEngine setupLocalVideo:canvas viewMode:HWRtcVideoDisplayModeFit];

  3. 加入房间。

     1 2 3 4 5 6 7 8 9101112
    HWRtcJoinParam *joinParam = [[HWRtcJoinParam alloc]init];NSString *authorization = @"";// 鉴权信息joinParam.role  = HWRtcRoleJoiner;joinParam.userId = @"HW"; // userId用于标识同一房间的不同用户joinParam.userName = @"HW";// 用户昵称,如无特殊需求,保持和userId一致即可joinParam.scenario = 1;joinParam.authorization = authorization;joinParam.ctime = time;joinParam.roomId = roomid;joinParam.autoSubscribeAudio = YES;//是否主动订阅音频joinParam.autoSubscribeVideo  = NO ;//默认-关闭BOOL result = [self.rtcEngine joinRoom:joinParam ];

    joinParam:入会参数,包含用户ID、用户名、房间号、认证信息、ctime、是否自动订阅音频和视频、SFU类型、场景和用户角色,具体请参见HWRtcJoinParam

  4. 监听远端用户加入房间,并设置远端窗口。

     1 2 3 4 5 6 7 8 9101112131415161718192021
    -(void)onRemoteUserOnline:(NSString*)roomId userId:(NSString*)userId userName:(NSString*)userName{    if([userId isEqualToString:localUid]){        return;    }         dispatch_async(dispatch_get_main_queue(),                          {                      UIView *videoView = [[UIVIew alloc] initWithFrame:CGRectMake(0, 0, 90, 160)];//iOS             //NSView *videoView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 90, 160)];//macOS             [self.view addSubview:videoView];              HWRtcVideoCanvas *canvas = [[HWRtcVideoCanvas alloc] init];                  canvas.uid  = userId;                canvas.view = videoView;              int result = [self.rtcEngine startRemoteStreamView:canvas  treamType:self.streamType];            if (result == 0) {                 [self.rtcEngine updateRemoteRenderMode:userId displayMode:HWRtcVideoDisplayModeFit mirrorMode:HWRtcVideoMirrorTypeDisable ];             }             [self.viewsArray addObject:canvas];        });    }

  5. 监听远端用户离开房间,并删除远端窗口。

     1 2 3 4 5 6 7 8 91011
    - (void)onRemoteUserOffline:(NSString *)roomId userId:(NSString *)userId reason:(NSInteger)reason{    dispatch_async(dispatch_get_main_queue(),    ^{        for (HWRtcVideoCanvas * canvas in self.viewsArray) {            if ([userId isEqualToString:canvas.uid]) {             [self.viewsArray removeObject:canvas];            }        }    });}

  6. 离开房间。

    [_rtcEngine leaveRoom];

support.huaweicloud.com/csdk-rtc/rtc_05_0049.html