如下是一个总体接入流程,详细细节请参见 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]; // 为了保证更好的语音聊天体验,profile 建议设置为 ByteRTCRoomProfileChatRoom config.profile = ByteRTCRoomProfileChatRoom; 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 |
---|---|
创建 ByteRTCVideo 实例 | createRTCVideo:delegate:parameters: |
创建 ByteRTCRoom 实例 | createRTCRoom |
开启麦克风采集 | startAudioCapture: |
关闭麦克风采集 | stopVideoCapture |
设置音频路由模式 | setDefaultAudioRoute: |
开启发言者音量监听 | enableAudioPropertiesReport: |
设置用户可见性 | setUserVisibility: |
加入 RTC 房间 | joinRoom:userInfo:roomConfig: |
离开 RTC 房间 | leaveRoom |
销毁房间对象 | destroy |
混音管理接口创建 | getMediaPlayer: |
开启混音播放 | open:config: |
关闭混音播放 | stop |
暂停混音播放 | pause |
恢复混音播放 | resume |
设置混音音量 | setVolume:volume: |
设置麦克风采集音量 | setCaptureVolume:volume: |
功能点 | 回调 |
---|---|
本地用户加入 RTC 回调 | rtcRoom:onRoomStateChanged:withUid:state:extraInfo: |
远端用户加入 RTC 回调 | rtcRoom:onUserJoined:elapsed: |
本地用户音量回调 | rtcEngine:onLocalAudioPropertiesReport: |
远端用户音量回调 | rtcEngine:onRemoteAudioPropertiesReport:totalRemoteVolume: |