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

场景搭建(iOS)

最近更新时间2024.04.11 14:39:16

首次发布时间2022.05.12 15:27:12

SDK集成

为了保证最佳体验效果,本场景需要集成火山引擎的RTC SDK 以及点播播放器 SDK 并且使用内容定制云服务,您需要在 RTC、点播、内容定制的控制台开通服务,相应开通指南如下:
1、RTC SDK总体接入流程,详细步骤请参看 RTC服务开通指南

alt

2、点播 SDK接入流程,详细步骤请参看点播控制台服务开通
3、内容定制接入流程,详细步骤请参看内容定制控制台服务开通

整体实现流程

整体业务流程图

alt

核心功能实现

创建/加入房间

时序图

alt

示例代码

/**
 * 加入RTC房间并初始化参数
 * @param token: RTC Token
 * @param roomID: RTC room id
 * @param uid: RTC user id
 **/
- (void)joinRTCRoomWithToken:(NSString *)token
                      roomID:(NSString *)roomID
                         uid:(NSString *)uid {
    // 初始化 ByteRTCVideo 对象
    self.rtcEngineKit = [ByteRTCVideo createRTCVideo:APPID
                                        delegate:self
                                      parameters:@{}];
    
    // 初始化 ByteRTCRoom 对象
    self.rtcRoom = [self.rtcEngineKit createRTCRoom:roomID];
    self.rtcRoom.delegate = self;
    
    // 设置为可见
    [self.rtcRoom setUserVisibility:YES];
    
    // 加入房间需要开启麦克风、相机,观众需要关闭麦克风、相机
    [self.rtcEngineKit startVideoCapture];
    [self.rtcEngineKit startAudioCapture];
        
    // 设置本地渲染和编码镜像
    [self.rtcEngineKit setLocalVideoMirrorType:ByteRTCMirrorTypeRenderAndEncoder];
   
    // 绑定本地视频预览视图
    UIView *view = [[UIView alloc] init];
    ByteRTCVideoCanvas *videoCanvas = [[ByteRTCVideoCanvas alloc] init];
    videoCanvas.uid = uid;
    videoCanvas.renderMode = ByteRTCRenderModeHidden;
    videoCanvas.view = view;
    [self.rtcEngineKit setLocalVideoCanvas:ByteRTCStreamIndexMain
                                withCanvas:videoCanvas];
    
    // 设置音频场景类型
    [self.rtcEngineKit setAudioScenario:ByteRTCAudioScenarioCommunication];
 
    // 开启发言者音量监听
    ByteRTCAudioPropertiesConfig *audioPropertiesConfig = [[ByteRTCAudioPropertiesConfig alloc] init];
    audioPropertiesConfig.interval = 300;
    [self.rtcEngineKit enableAudioPropertiesReport:audioPropertiesConfig];
    
    // 加入房间,开始连麦,需要申请AppId和Token
    ByteRTCUserInfo *userInfo = [[ByteRTCUserInfo alloc] init];
    userInfo.userId = uid;
    ByteRTCRoomConfig *config = [[ByteRTCRoomConfig alloc] init];
    config.profile = ByteRTCRoomProfileLwTogether;
    config.isAutoPublish = YES;
    config.isAutoSubscribeAudio = YES;
    config.isAutoSubscribeVideo = 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)rtcRoom:(ByteRTCRoom *)rtcRoom onUserJoined:(ByteRTCUserInfo *)userInfo elapsed:(NSInteger)elapsed {
    // 收到远端用户加入房间回调
}

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

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

/**
 * 房间内新增远端摄像头/麦克风采集的媒体流的回调
 */
- (void)rtcRoom:( ByteRTCRoom *)rtcRoom onUserPublishStream:(NSString *)userId type:(ByteRTCMediaStreamType)type {
    if (type == ByteRTCMediaStreamTypeAudio) {
        return;
    }
    // 绑定远端用户视频视图 
    UIView *view = [[UIView alloc] init];
    ByteRTCVideoCanvas *videoCanvas = [[ByteRTCVideoCanvas alloc] init];
    videoCanvas.renderMode = ByteRTCRenderModeHidden;
    videoCanvas.view = view;
    
    ByteRTCRemoteStreamKey *streamKey = [[ByteRTCRemoteStreamKey alloc] init];
    streamKey.userId = userId;
    streamKey.roomId = self.rtcRoom.getRoomId;
    streamKey.streamIndex = ByteRTCStreamIndexMain;
    
    [self.rtcEngineKit setRemoteVideoCanvas:streamKey withCanvas:videoCanvas];
}

一起看抖音

注意:内容服务的接入及详细使用,可参考: 应用管理手册

时序图

alt

示例代码

/**
 * 开启PCM混音
 * @param mixId 混音ID。用于标识混音,保证混音 ID 唯一性。 如果使用相同的 ID 重复调用本*方法后,前一次混音会停止,后一次混音开始。
 */
- (void)startAudioMixing:(int)mixId {
    // 开启本地混音播放
    ByteRTCMediaPlayer *mediaPlayer = [self.rtcEngineKit getMediaPlayer:0];
    ByteRTCMediaPlayerCustomSource *mdsSource = [[ByteRTCMediaPlayerCustomSource alloc] init];
    mdsSource.provider = (id<ByteRTCMediaPlayerCustomSourceProvider>)source;
    mdsSource.mode = ByteRTCMediaPlayerCustomSourceModePull;
    mdsSource.type = ByteRTCMediaPlayerCustomSourceStreamTypeEncoded;

    ByteRTCMediaPlayerConfig *config = [[ByteRTCMediaPlayerConfig alloc] init];
    config.type = ByteRTCAudioMixingTypePlayout;
    config.playCount = 1;
    config.startPos = startPos;
    config.callbackOnProgressInterval = self.ktvInfo.callbackOnProgressInterval;
    config.syncProgressToRecordFrame = YES;
    config.autoPlay = NO;
    
    [mediaPlayer openWithCustomSource:self.mdsSource config:config];

}

/** 
 * 播放器音频数据回调,推送音频数据到RTC
 */
- (void)pushAudioMixingFrame:(int)mixId audioFrame:(ByteRTCAudioFrame *)audioFrame {
    // 将播放器回调出的数据送入RTC播放
    ByteRTCMediaPlayer *mediaPlayer = [self.rtcEngineKit getMediaPlayer:0];
    [mediaPlayer pushExternalAudioFrame:audioFrame];
}

/**
* 关闭PCM混音
*/
- (void)stopAudioMixing:(int)mixId {
    // 关闭混音
    ByteRTCMediaPlayer *mediaPlayer = [self.rtcEngineKit getMediaPlayer:0];
    [mediaPlayer stop];
}

/**
* 发送房间广播消息
*/
- (void)sendRoomMessage:(NSString *)message {
    // 发送同步消息
    // 房主讲当前视频的播放进度及播放状态发送给房间内所有观众
    [self.rtcRoom sendRoomMessage:string];
}
- (void)rtcRoom:(ByteRTCRoom *)rtcRoom onRoomMessageReceived:(NSString *)uid message:(NSString *)message {
    // 收到同步消息
    // 观众收到房主发送的播放状态与本地播放状态进行对比并修正,保持同步
}

控制台

alt

/** 
 * 调节混音的音量大小,包括音频文件混音和 PCM 混音。
 * @param mixId 混音 ID
 * @param volume 混音音量相对原音量的比值。范围为 `[0, 400]`,建议范围是 `[0, 100]`。
 */
- (void)setAudioMixingVolume:(int)mixId volume:(int)volume {
    ByteRTCMediaPlayer *mediaPlayer = [self.rtcEngineKit getMediaPlayer:0];
    [mediaPlayer setVolume:volume type:ByteRTCAudioMixingTypePlayout];
}

/** 
 * 调节本地播放的所有远端用户混音后的音量。播放音频前或播放音频时,你都可以使用此接口设定播放音量。
 * @param volume 音频播放音量值和原始音量的比值,范围是 [0, 400],单位为 %,自带溢出保护。
 */
- (void)setPlaybackVolume:(NSInteger)volume {
    [self.rtcEngineKit setPlaybackVolume:volume];
}

核心 API 与回调参考

API

功能点API
创建 RTCVideo 对象createRTCVideo:delegate:parameters:
创建 RTCRoom 对象createRTCRoom:
设置用户可见性setUserVisibility:
设置本地渲染和编码镜像setLocalVideoMirrorType:
设置本地视频渲染视图setLocalVideoCanvas:withCanvas
设置音频场景类型setAudioScenario:
启用音频信息提示enableAudioPropertiesReport:
开启本地音频采集startAudioCapture
开启本地视频采集startVideoCapture
加入RTC 房间joinRoom:userInfo:roomConfig:
设置远端用户视频渲染视图setRemoteVideoCanvas:withCanvas:
开启PCM 音频数据混音openWithCustomSource:config:
推送 PCM 音频帧数据用于混音pushExternalAudioFrame:
调节混音的音量大小setVolume:volume:
给房间内的所有其他用户发送文本消息sendRoomMessage:
调节远端用户用通话音量setPlaybackVolume:
打开音量闪避功能enablePlaybackDucking:
离开房间leaveRoom
销毁 RTCRoom 对象destroy
关闭内部音频采集stopAudioCapture
关闭内部视频采集stopVideoCapture
销毁 RTCVideo 对象destroy

回调

功能点回调
本地用户加入 RTC 房间回调rtcRoom:onRoomStateChanged:uid:state:extraInfo
音频播放路由变化时,收到该回调rtcEngine:onAudioRouteChanged:
本地用户音量回调rtcEngine:onLocalAudioPropertiesReport:
远端用户音量回调rtcEngine:onRemoteAudioPropertiesReport:totalRemoteVolume:
远端用户加入 RTC 回调rtcRoom:onUserJoined:elapsed:
房间内新增远端媒体流回调rtcRoom:onUserPublishStream:type:
收到房间内广播文本消息回调rtcRoom:onRoomMessageReceived:message: