文档首页 > > SDK参考> iOS SDK> SDK使用

SDK使用

分享
更新时间:2020/10/21 GMT+08:00
  1. 创建引擎并初始化,其中域名请保持默认值即可,AppId提交工单申请。

    1
    2
    _rtcEngine = [HWRtcEngine sharedEngine];
    [_rtcEngine init:domain appId:appid];
    

  2. 设置本地窗口。

    1
    2
    3
    4
    5
    6
    _localVideoSession = [[HWVideoSession alloc] initWithUserID:self.userName];
    HwRtcVideoView *videoView = [[HwRtcVideoView alloc] initWithFrame:self.view.bounds];
    _localVideoSession.videoView = videoView;
    _localVideoSession.userName= self.userName;
    _localVideoSession.isPlaying = YES;
    [_rtcEngine setupLocalVideo:_localVideoSession.canvas viewMode:self.displayMode];
    

  3. 加入房间。

    1
    2
    3
    4
    5
    HWRtcUserInfo *userInfo = [[HWRtcUserInfo alloc]init];
    userInfo.role = cameraVC.roleType;
    userInfo.userId = cameraVC.userName;
    userInfo.userName = cameraVC.userName;
    BOOL result = [self.rtcEngine joinRoom:userInfo roomId:cameraVC.roomID mediaType:HWRtcMediaTypeVideo];
    

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

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    -(void)onUserJoined:(NSString*)roomId userId:(NSString*)userId nickName:(NSString*)nickName{
        if([userId isEqualToString:localUid] || self.roleType ==HWRtcRolePublisher){
            return;
        }
        @synchronized (self) {
             dispatch_async(dispatch_get_main_queue(),
                              ^{         
                 HWVideoSession *videoSession = [[HWVideoSession alloc] initWithUserID:userId];
                 videoSession.userName = userId;
                 HwRtcVideoView *videoView = [[HwRtcVideoView alloc] initWithFrame:CGRectMake(0, 0, 90, 160)];
                 videoSession.videoView = videoView;
                 videoSession.isPlaying = YES;
                 [self.cameraView addSubview:videoView];
                 int result = [self.rtcEngine startRemoteStreamView:videoSession.canvas treamType:self.streamType];
                if (result == 0) {
                     [self.rtcEngine setRemoteViewDisplayMode:self.displayMode userid:userId];
                 }
                 [self.viewsArray addObject:videoSession];
    
            });
         }
    }
    

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

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    - (void)onUserOffline:(NSString *)roomId userId:(NSString *)userId reason:(NSInteger)reason
    {
        if(self.roleType == HWRtcRolePublisher){
            return;
        }
        dispatch_async(dispatch_get_main_queue(),
        ^{
            NSInteger index = -1;
            for (HWVideoSession*offlineVideoSession in self.viewsArray) {
                if ([userId isEqualToString:videoSession.canvas.uid]) {
                 [self.viewsArray removeObject:offlineVideoSession];
                }
            }
        });
    }
    

  6. 离开房间。

    [_rtcEngine leaveRoom];

分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!非常感谢您的反馈,我们会继续努力做到更好!
反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

智能客服提问云社区提问