You need to enable JavaScript to run this app.
导航
场景搭建(iOS)
最近更新时间:2023.12.28 20:05:12首次发布时间:2022.11.15 18:43:11

SDK集成

如下是一个总体接入流程,详细细节请参见 RTC服务开通指南

alt

整体实现流程

整体业务流程图

alt

核心功能实现

进入退出房间

时序图

alt

示例代码

- (void)joinRTCRoomWithModel:(GroupVideoCallRoomUserModel *)localUserModel
                    rtcToken:(NSString *)rtcToken {
    
    // 初始化 RTC Video 对象
    // Initialize the RTC Video object
    self.rtcRTCVideo = [ByteRTCVideo createRTCVideo:appID
                                           delegate:self
                                         parameters:@{}];
    
    // 初始化 RTC Room 对象
    // Initialize the RTC Room object
    self.rtcRoom = [self.rtcRTCVideo createRTCRoom:roomId];
    self.rtcRoom.delegate = self;
    
    // 开启麦克风采集
    // Enable microphone capture
    [self.rtcRTCVideo startAudioCapture];
    
    // 根据预览页配置开启/关闭相机采集
    // Enable/disable camera capture according to the preview page configuration
    if (preSetting.isEnableVideo) {
        [self.rtcRTCVideo startVideoCapture];
    } else {
        [self.rtcRTCVideo stopVideoCapture];
    }
    
    // 根据预览页配置开启/关闭音频推送
    // Enable/disable audio push according to the preview page configuration
    if (preSetting.isEnableAudio) {
        [self.rtcRoom publishStream:ByteRTCMediaStreamTypeAudio];
    } else {
        [self.rtcRoom unpublishStream:ByteRTCMediaStreamTypeAudio];
    }
    
    //设置音频路由模式,扬声器/听筒
    //Set the audio routing mode, speaker/earpiece
    [self.rtcRTCVideo setAudioPlaybackDevice:preSetting.playback];
    
    //开启/关闭发言者音量键控
    //Turn on/off speaker volume keying
    ByteRTCAudioPropertiesConfig *audioPropertiesConfig = [[ByteRTCAudioPropertiesConfig alloc] init];
    audioPropertiesConfig.interval = 1000;
    [self.rtcRTCVideo enableAudioPropertiesReport:audioPropertiesConfig];
    
    //加入房间,开始连麦,需要申请AppId和Token
    //Join the room, start connecting the microphone, you need to apply for AppId and Token
    ByteRTCUserInfo *userInfo = [[ByteRTCUserInfo alloc] init];
    userInfo.userId = localUserModel.uid;
    NSDictionary *extraInfo = @{
        @"user_name": localUserModel.name,
        @"user_id": localUserModel.uid
    };
    userInfo.extraInfo = [extraInfo yy_modelToJSONString];
    ByteRTCRoomConfig *config = [[ByteRTCRoomConfig alloc] init];
    config.profile = ByteRTCRoomProfileCommunication;
    // 开启自动推送
    // enable automatic push
    config.isAutoPublish = YES;
    // 开启自动订阅音视频
    // Enable automatic subscription of audio and video
    config.isAutoSubscribeAudio = YES;
    config.isAutoSubscribeVideo = YES;
    [self.rtcRoom joinRoom:rtcToken userInfo:userInfo roomConfig:config];
}

- (void)leaveRTCRoom {
    //离开频道
    //Leave the channel
    [self.rtcRTCVideo stopAudioCapture];
    [self.rtcRoom leaveRoom];
    [self.rtcRoom destroy];
    self.rtcRoom = nil;
}

断网重连

  • 短时间断网
    无需处理。RTC SDK 会补发断网期间丢失的消息。
    例如:本地用户网络从 WIFI 切换到 5G,在网络切换中有远端用户加入房间。当本地用户网络切换成功后,就会收到 RTC SDK -rtcRoom:onUserJoined:elapsed: 回调。
  • 长时间断网,时序图和关键代码如下:

时序图

alt

示例代码

- (void)rtcRoom:(ByteRTCRoom *)rtcRoom onRoomStateChanged:(NSString *)roomId withUid:(NSString *)uid state:(NSInteger)state extraInfo:(NSString *)extraInfo {
    NSDictionary *dic = [self dictionaryWithJsonString:extraInfo];
    NSInteger joinType = -1;
    if ([dic isKindOfClass:[NSDictionary class]]) {
        NSString *joinTypeStr = [NSString stringWithFormat:@"%@", dic[@"join_type"]];
        joinType = joinTypeStr.integerValue;
    }
    // joinType '0'为首次进房,'1'为重连进房 ;state '0'为加入房间成功
    // joinType '0' is the first time entering the room, '1' is the reconnection entering the room; state '0' means join the room successfully
    if (state == 0 && joinType == 1) {
        // 执行业务重连API,更新业务状态
        // Execute business re-API, update business status
        [Networking reconnectBlock:^(RTMACKModel * _Nonnull model) {
            // do something
        }];
    }
}

屏幕共享

屏幕共享参看iOS 端屏幕共享

核心功能 API 与回调参考

API

功能点API
创建 ByteRTCVideo 实例createRTCVideo:delegate:parameters:
设置视频发布参数SetVideoEncoderConfig:
开启本地音频采集startAudioCapture:
开启本地视频采集startVideoCapture
设置本地视频渲染setLocalVideoCanvas:withCanvas:
加入 RTC 房间joinRoom:userInfo:roomConfig:
设置视频渲染视图setRemoteVideoCanvas:withCanvas:
离开房间leaveRoom
关闭内部音频采集stopAudioCapture
关闭内部视频采集stopVideoCapture
销毁引擎实例对象destroy
发布本地通过摄像头/麦克风采集的媒体流publishStream:
取消发布本地通过摄像头/麦克风采集的媒体流unpublishStream:
设置音频播放设备为扬声器或者听筒setAudioRoute:
开启音量提示enableAudioPropertiesReport:
开启镜像setLocalVideoMirrorType:
设置音质档位setAudioProfile:
切换视前置/后置摄像头switchCamera:

回调

功能点回调
本地进房成功回调rtcRoom:onRoomStateChanged:uid:state:extraInfo
远端可见用户加入房间rtcRoom:onUserJoined:elapsed: