文档首页 > > 客户端SDK参考> iOS SDK>

SDK使用

SDK使用

分享
更新时间:2021/01/07 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 userName:(NSString*)userName{
        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
    - (void)onUserOffline:(NSString *)roomId userId:(NSString *)userId reason:(NSInteger)reason
    {
        if(self.roleType == HWRtcRolePublisher){
            return;
        }
        dispatch_async(dispatch_get_main_queue(),
        ^{
            for (HWVideoSession* offlineVideoSession in self.viewsArray) {
                if ([userId isEqualToString:videoSession.canvas.uid]) {
                 [self.viewsArray removeObject:offlineVideoSession];
                }
            }
        });
    }
    

  6. 离开房间。

    [_rtcEngine leaveRoom];

分享:

    相关文档

    相关产品