You need to enable JavaScript to run this app.
导航

场景搭建(iOS)

最近更新时间2023.08.28 12:05:30

首次发布时间2022.06.16 16:17:19

SDK集成

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

alt

整体实现流程

整体业务流程图

alt

  • 房主可以邀请观众上麦、下麦嘉宾、封锁麦位。
  • 房主可以播放背景音乐、修改音乐音量、人声音量。
  • 房主可以开关观众连线申请开关。开启开关后,观众申请上麦需要房主同意。
  • 观众可以申请上麦、下麦。

核心功能实现

创建/加入房间模块

时序图

alt

示例代码

/**
 * 加入RTC房间并初始化参数
 * @param token: RTC Token
 * @param roomID: RTC room id
 * @param uid: RTC user id
 * @param isHost: YES 业务上主播 ; NO 业务上观众
 **/
- (void)joinRTCRoomWithToken:(NSString *)token
                      roomID:(NSString *)roomID
                         uid:(NSString *)uid
                      isHost:(NSString *)isHost {
    // 初始化 ByteRTCVideo 对象
    self.rtcEngineKit = [ByteRTCVideo createRTCVideo:appID
                                            delegate:self
                                          parameters:@{}];
    
    // 初始化 ByteRTCRoom 对象
    self.rtcRoom = [self.rtcEngineKit createRTCRoom:roomID];
    self.rtcRoom.delegate = self;
    
    // 主播开启麦克风采集,观众关闭麦克风采集
    if (isHost) {
        [self.rtcEngineKit startAudioCapture];
    } else {
        [self.rtcEngineKit stopAudioCapture];
    }
 
    // 设置音频路由模式
    [self.rtcEngineKit setDefaultAudioRoute:ByteRTCAudioRouteSpeakerphone];
 
    // 开启发言者音量监听(用于展示声波动效)
    ByteRTCAudioPropertiesConfig *audioPropertiesConfig = [[ByteRTCAudioPropertiesConfig alloc] init];
    audioPropertiesConfig.interval = 300;
    [self.rtcEngineKit enableAudioPropertiesReport:audioPropertiesConfig];
    
    // 设置主播为可见状态,设置观众为隐身状态
    [self.rtcRoom setUserVisibility:isHost ? YES : NO];
 
    // 加入房间,开始连麦需要申请AppId和Token
    ByteRTCUserInfo *userInfo = [[ByteRTCUserInfo alloc] init];
    userInfo.userId = uid;
    ByteRTCRoomConfig *config = [[ByteRTCRoomConfig alloc] init];
    config.profile = ByteRTCRoomProfileInteractivePodcast;
    config.isAutoPublish = YES;
    config.isAutoSubscribeAudio = YES;
    [self.rtcRoom joinRoom:token userInfo:userInfo roomConfig:config];
}
- (void)rtcRoom:(ByteRTCRoom *)rtcRoom onRoomStateChanged:(NSString *)roomId
                                                  withUid:(NSString *)uid
                                                    state:(NSInteger)state
                                                extraInfo:(NSString *)extraInfo {
    // 收到 RTC 加入房间结果
}

- (void)rtcEngine:(ByteRTCVideo *)engine onLocalAudioPropertiesReport:(NSArray<ByteRTCLocalAudioPropertiesInfo *> *)audioPropertiesInfos {
    // 本地用户音量回调    
}

- (void)rtcEngine:(ByteRTCVideo *)engine onRemoteAudioPropertiesReport:(NSArray<ByteRTCRemoteAudioPropertiesInfo *> *)audioPropertiesInfos 
                                                     totalRemoteVolume:(NSInteger)totalRemoteVolume {
    // 远端用户音量回调
}

上麦模块

时序图

alt

示例代码

// 观众上麦/下麦
- (void)makeCoHost:(BOOL)isCoHost {
    if (isCoHost) {
        [self.rtcEngineKit startAudioCapture];
        [self.rtcRoom setUserVisibility:YES];
        [self.rtcRoom publishStream:ByteRTCMediaStreamTypeAudio];
    } else {
        [self.rtcEngineKit stopAudioCapture];
        [self.rtcRoom setUserVisibility:NO];
        [self.rtcRoom unpublishStream:ByteRTCMediaStreamTypeAudio];
    }
}

背景音乐

时序图

alt

示例代码

- (void)startBackgroundMusic:(NSString *)filePath {
    // 获取 ByteRTCAudioMixingManager 对象
    ByteRTCAudioMixingManager *audioMixingManager = [self.rtcEngineKit getAudioMixingManager];
    
    // 开启混音播放
    ByteRTCAudioMixingConfig *config = [[ByteRTCAudioMixingConfig alloc] init];
    config.type = ByteRTCAudioMixingTypePlayoutAndPublish;
    config.playCount = -1;
    [audioMixingManager startAudioMixing:_audioMixingID filePath:filePath config:config];
}

- (void)setRecordingVolume:(NSInteger)volume {
    // 设置麦克风采集音量
    [self.rtcEngineKit setCaptureVolume:ByteRTCStreamIndexMain volume:(int)volume];
}
 
- (void)setMusicVolume:(NSInteger)volume {
    // 设置混音音乐音量
    ByteRTCAudioMixingManager *audioMixingManager = [self.rtcEngineKit getAudioMixingManager];
    [audioMixingManager setAudioMixingVolume:_audioMixingID 
                                      volume:(int)volume 
                                        type:ByteRTCAudioMixingTypePlayoutAndPublish];
}

核心功能 API 与回调参考

API

功能点API
创建 ByteRTCVideo 实例createRTCVideo:delegate:parameters:
创建 ByteRTCRoom 实例createRTCRoom
开启麦克风采集startAudioCapture:
关闭麦克风采集stopVideoCapture
设置音频路由模式setDefaultAudioRoute:
开启发言者音量监听enableAudioPropertiesReport:
设置用户可见性setUserVisibility:
加入 RTC 房间joinRoom:userInfo:roomConfig:
离开 RTC 房间leaveRoom
销毁房间对象destroy
混音管理接口创建getAudioMixingManager
开启混音播放startAudioMixing:filePath:config:
关闭混音播放stopAudioMixing:
暂停混音播放pauseAudioMixing:
恢复混音播放resumeAudioMixing:
设置混音音量setAudioMixingVolume:volume:type:
设置麦克风采集音量setCaptureVolume:volume:

回调

功能点回调
本地用户加入 RTC 回调rtcRoom:onRoomStateChanged:withUid:state:extraInfo:
远端用户加入 RTC 回调rtcRoom:onUserJoined:elapsed:
本地用户音量回调rtcEngine:onLocalAudioPropertiesReport:
远端用户音量回调rtcEngine:onRemoteAudioPropertiesReport:totalRemoteVolume: