加入和离开 RTC 房间

更新时间: 2022/11/04 10:02:17

本文介绍在 K 歌场景中用户加入房间和离开 RTC 房间的操作。

背景信息

KTV 房间在底层实现上包括 IM 的聊天室以及 RTC 房间。

IM 聊天室的实现方法请参见聊天室

本文主要展示 RTC 房间的相关实现。

实现流程

  1. 在加入房间前调用setChannelProfile接口,设置房间场景为直播场景(LiveBroadcasting)。

  2. 调用 setAudioProfile接口,设置音频 profile 类型为 HighQualityStereo,设置 scenarioMUSIC

  3. 调用 joinChannel 接口加入 KTV 房间。

    第一位用户加入 RTC 房间后,RTC 服务器会自动创建一个 RTC 房间。

  4. 调用 leaveChannel 接口离开 KTV 房间。

示例代码

NERtcEngineContext *context = [[NERtcEngineContext alloc] init];
context.appKey = kNertcAppkey;
context.engineDelegate = self;

NERtcEngine *coreEngine = [NERtcEngine sharedEngine];
[coreEngine setupEngineWithContext:context];
[coreEngine setChannelProfile:kNERtcChannelProfileLiveBroadcasting];
[coreEngine setAudioProfile:kNERtcAudioProfileHighQualityStereo scenario:kNERtcAudioScenarioMusic];
[coreEngine setAudioFrameObserver:self];

//串行合唱,混音时,主唱和副唱的的音频格式需要一致
NERtcAudioFrameRequestFormat *mixedAudioFormat = [[NERtcAudioFrameRequestFormat alloc] init];
mixedAudioFormat.channels = 2;
mixedAudioFormat.sampleRate = 48000;
mixedAudioFormat.mode = kNERtcAudioFrameOpModeReadOnly;
[coreEngine setMixedAudioFrameParameters:mixedAudioFormat];

//串行合唱,混音时,主唱和副唱的的音频格式需要一致
NERtcAudioFrameRequestFormat *recordingAudioFormat = [[NERtcAudioFrameRequestFormat alloc] init];
recordingAudioFormat.channels = 2;
recordingAudioFormat.sampleRate = 48000;
recordingAudioFormat.mode = kNERtcAudioFrameOpModeReadWrite;
[coreEngine setRecordingAudioFrameParameters:aFormat];

//设置成功后,设备会多次与 RTC 服务器校准 NTP 值,该配置能让 getNtpTimeOffset 的值更加精准,实现实时合唱主唱和副唱伴奏精准同步。
[coreEngine setStreamAlignmentProperty:YES];

[coreEngine joinChannelWithToken:token channelName:channelName myUid:uId completion:^(NSError * _Nullable error, uint64_t channelId, uint64_t elapesd, uint64_t uid) {
                                          }];
[[NERtcEngine sharedEngine] leaveChannel];
此文档是否对你有帮助?
有帮助
去反馈
  • 背景信息
  • 实现流程
  • 示例代码