SDK集成
如下是一个总体接入流程,详细细节请参见 RTC服务开通指南
整体实现流程
整体业务流程图
- 房主可以邀请观众上麦、下麦嘉宾、封锁麦位。
- 房主可以播放背景音乐、修改音乐音量、人声音量。
- 房主可以开关观众连线申请开关。开启开关后,观众申请上麦需要房主同意。
- 观众可以申请上麦、下麦。
核心功能实现
创建/加入房间模块
时序图
示例代码
/**
* 加入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 {
// 远端用户音量回调
}
上麦模块
时序图
示例代码
// 观众上麦/下麦
- (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];
}
}
背景音乐
时序图
示例代码
- (void)startBackgroundMusic:(NSString *)filePath {
// 获取 ByteRTCMediaPlayer 对象
ByteRTCMediaPlayer *mediaPlayer = [self.rtcEngineKit getMediaPlayer:0];
// 开启混音播放
ByteRTCMediaPlayerConfig *config = [[ByteRTCMediaPlayerConfig alloc]init];
config.type = ByteRTCAudioMixingTypePlayoutAndPublish;
config.playCount = -1;
config.autoPlay = YES;
[mediaPlayer open:filePath config:config];
}
- (void)setRecordingVolume:(NSInteger)volume {
// 设置麦克风采集音量
[self.rtcEngineKit setCaptureVolume:ByteRTCStreamIndexMain volume:(int)volume];
}
- (void)setMusicVolume:(NSInteger)volume {
// 设置混音音乐音量
ByteRTCMediaPlayer *mediaPlayer = [self.rtcEngineKit getMediaPlayer:0];
[mediaPlayer setVolume:volume type:ByteRTCAudioMixingTypePlayoutAndPublish];
}
核心功能 API 与回调参考
API
回调