文档

iOS SDK集成

更新时间:

本文介绍K歌房单人演唱模式iOS SDK集成的代码示例及集成说明。

功能时序图

image.svg

主播创建房间

/* 实现AliRtcEngineDelegate 监听回调 */
_engine = [AliRtcEngine sharedInstance:self extras:extras];

//设置频道模式
[self.engine setChannelProfile:AliRtcInteractivelive];
//设置用户角色
[self.engine setClientRole:AliRtcClientRoleInteractive];
/* 使用高音质和ktv场景 */
[self.engine setAudioProfile:AliRtcEngineHighQualityMode audio_scene:AliRtcSceneKtvMode];

//拉流设置
[self.engine setDefaultSubscribeAllRemoteAudioStreams:YES];
[self.engine subscribeAllRemoteAudioStreams:YES];
[self.engine publishLocalAudioStream:YES];
[self.engine setAudioOnlyMode:YES];

/* 是否听筒播放 */
[self.engine enableSpeakerphone:ctrl.useSpeaker];

NSMutableDictionary *raw_token = [[NSMutableDictionary alloc] init];
[raw_token setValue:info.appId forKey:@"appid"];
[raw_token setValue:info.channelId forKey:@"channelid"];
[raw_token setValue:info.userId forKey:@"userid"];
[raw_token setValue:info.nonce forKey:@"nonce"];
[raw_token setValue:@(info.timestamp) forKey:@"timestamp"];
[raw_token setValue:info.gslb forKey:@"gslb"];
[raw_token setValue:info.token forKey:@"token"];
NSData *token_data = [NSJSONSerialization dataWithJSONObject:raw_token options:NSJSONWritingPrettyPrinted error:nil];
NSString *token_str = [token_data base64EncodedStringWithOptions:0];
                
[self.engine joinChannel:token_str channelId:nil userId:nil name:nil onResultWithUserId:^(NSInteger errCode, NSString * _Nonnull channel, NSString * _Nonnull userId, NSInteger elapsed) {
    NSString *sting = [NSString stringWithFormat:@"joinRst: %d", (int)errCode];
    [weakSelf log:sting];
    if(errCode != 0 && ![weakSelf.engine isInCall]){
        weakSelf.callState = YES; // restore gui
    }else{
        weakSelf.callState = NO; //入会成功
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf addTableView]; //渲染远端视图
        });
    }
}];

观众加入房间

和主播创建房间调用接口相同,根据是否上麦,设置角色信息,以观众为示例:

/* 实现AliRtcEngineDelegate 监听回调 */
_engine = [AliRtcEngine sharedInstance:self extras:extras];

//设置频道模式
[self.engine setChannelProfile:AliRtcInteractivelive];
//设置用户角色
[self.engine setClientRole:AliRtcClientRoleInteractive];
/* 使用高音质和ktv场景 */
[self.engine setAudioProfile:AliRtcEngineHighQualityMode audio_scene:AliRtcSceneKtvMode];
//拉流设置
[self.engine setDefaultSubscribeAllRemoteAudioStreams:YES];
[self.engine subscribeAllRemoteAudioStreams:YES];
[self.engine publishLocalAudioStream:YES];
[self.engine setAudioOnlyMode:YES];

/* 是否听筒播放 */
[self.engine enableSpeakerphone:ctrl.useSpeaker];

NSMutableDictionary *raw_token = [[NSMutableDictionary alloc] init];
[raw_token setValue:info.appId forKey:@"appid"];
[raw_token setValue:info.channelId forKey:@"channelid"];
[raw_token setValue:info.userId forKey:@"userid"];
[raw_token setValue:info.nonce forKey:@"nonce"];
[raw_token setValue:@(info.timestamp) forKey:@"timestamp"];
[raw_token setValue:info.gslb forKey:@"gslb"];
[raw_token setValue:info.token forKey:@"token"];
NSData *token_data = [NSJSONSerialization dataWithJSONObject:raw_token options:NSJSONWritingPrettyPrinted error:nil];
NSString *token_str = [token_data base64EncodedStringWithOptions:0];
                
[self.engine joinChannel:token_str channelId:nil userId:nil name:nil onResultWithUserId:^(NSInteger errCode, NSString * _Nonnull channel, NSString * _Nonnull userId, NSInteger elapsed) {
    NSString *sting = [NSString stringWithFormat:@"joinRst: %d", (int)errCode];
    [weakSelf log:sting];
    if(errCode != 0 && ![weakSelf.engine isInCall]){
        weakSelf.callState = YES; // restore gui
    }else{
        weakSelf.callState = NO; //入会成功
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf addTableView]; //渲染远端视图
        });
    }
}];

演唱者点歌开始演唱

AliRtcExternalAudioStreamConfig *config = [AliRtcExternalAudioStreamConfig new];
config.channels = _pcmLocalChannels;
config.sampleRate = _pcmLocalSampleRate;
/* 如果需要外放出来声音,设置外放音量 */
config.publishVolume = 60;
config.playoutVolume = 100;
/* 该stream id需要存下来,用于添加pcm数据和删除流 */
_externalPlayoutStreamId = [self.engine addExternalAudioStream:config];

演唱者发送歌曲进度和播放伴奏

/* 送入pcm数据 */ 
AliRtcAudioFrame *sample = [AliRtcAudioFrame new];
sample.dataPtr = _pcmLocalData;
sample.samplesPerSec = _pcmLocalSampleRate;
sample.bytesPerSample = sizeof(int16_t);
sample.numOfChannels = _pcmLocalChannels;
sample.numOfSamples = numOfSamples;
int rc = [self.engine pushExternalAudioStream:_externalPlayoutStreamId rawData:sample];
/* 发送datachannel 进度信息 */
AliRtcDataChannelMsg* msg = [[AliRtcDataChannelMsg alloc] init];
msg.type = AliRtcDataMsgCustom;
msg.networkTime = [self.engine getNetworkTime];
msg.data =  [NSData dataWithBytes:&progress length:8];
[self.engine sendDataChannelMessage:msg];

观众侧接受歌曲进度

- (void)onDataChannelMessage:(NSString *_Nonnull)uid controlMsg:(AliRtcDataChannelMsg*_Nonnull)controlMsg {
    long long progress = 0;
    [controlMsg.data getBytes:&progress length:sizeof(time)];
}

主播停止歌曲

[self.engine removeExternalAudioStream:_externalPlayoutStreamId];
/* 停止歌曲解码、IM通知其他人 */
  • 本页导读 (1)
文档反馈