网易互动直播聊天室(包含用户上麦下麦踢出聊天室)

1.先前准备

1).http://netease.im/im-sdk-demo附上sdk下载链接

2).导入sdk 建议手动导入 并添加相应的依赖库文档都有

2.写代码

1).在网易云信登录获取到信息AppDelegate导入#import

NSString *appKey = @"30**************6e";

NSString *cerName = @"https://*********api";

[[NIMSDK sharedSDK] registerWithAppID:appKey cerName:cerName];

并获取到云信id这是必须的,先根据用户ID获取到token,获取后调取方法[[[NIMSDK sharedSDK]loginManager] login:用户ID token:在后台绑定的登录token completion:^(NSError * _Nullable error) {

if (error) {

NSLog(@"登录失败%ld", error.code);

} else {

NSLog(@"登录成功%d", 200);

}

}]

2).实现该功能页面导入#import

2.0).创建一个会议NIMNetCallMeeting *_currentMeeting;

2.1).判断是否是主持人或者用户实现不同的方法

网易互动直播聊天室(包含用户上麦下麦踢出聊天室)_第1张图片

2.2)主持人创建会议-(void)createVideoAndAudio

{

NIMNetCallMeeting *meeting = [[NIMNetCallMeeting alloc] init];

meeting.name = 聊天室ID;

[self joinNetCallWithRMeeting:meeting];

}

- (void)joinNetCallWithRMeeting:(NIMNetCallMeeting *)rMeeting

{

if ([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)]) {

[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {

if (granted) {

NIMNetCallOption *callOption =[[NIMNetCallOption alloc]init];

// 发送视频质量(SDK会处理 有可能无效)

//                callOption.preferredVideoQuality =NIMNetCallVideoQualityDefault;

// 禁止剪裁

//                callOption.disableVideoCropping =NO;

// 自动旋转远端画面

callOption.autoRotateRemoteVideo =YES;

// 期望的视频编码器

callOption.preferredVideoEncoder =NIMNetCallVideoCodecDefault;

// 期望的视频解码器

callOption.preferredVideoDecoder =NIMNetCallVideoCodecDefault;

// 是否使用后置摄像头

//                callOption.startWithBackCamera =NO;

// 启用互动直播

callOption.enableBypassStreaming =YES;

// 互动直播推流地址

callOption.bypassStreamingUrl = self.url;

// 是否在服务器录制音频

callOption.serverRecordAudio =YES;

callOption.autoDeactivateAudioSession = NO;

// 是否在服务器录制视频

callOption.serverRecordVideo =YES;

// 加入会议的类型->音频/视频

rMeeting.type = NIMNetCallMediaTypeAudio;

// 是否发送音视频数据

rMeeting.actor = YES;

// 网路电话配置

rMeeting.option = callOption;

//                NSLog(@"错那了%@",rMeeting);

//进入房间聊天

[[NIMAVChatSDK sharedSDK].netCallManager joinMeeting:rMeeting completion:^(NIMNetCallMeeting * _Nonnull meeting, NSError * _Nonnull error) {

if (!error) {

[[FbwHttpClient shareInstance]startRequestMethod:POST parameters:@{@"userId":用户ID,@"chatRoomId":聊天室ID} url:UrL_JoinChatRoom success:^(id responseObject) {

if ([[responseObject objectForKey:@"errcode"]integerValue] != 0) {

[SVProgressHUD showErrorWithStatus:[responseObject objectForKey:@"msg"]];

}else{

}

} failure:^(NSError *error) {

}];

NSLog(@"进入成功");

//默认关闭所有人视频

[[NIMAVChatSDK sharedSDK].netCallManager setCameraDisable:YES];

[[NIMAVChatSDK sharedSDK].netCallManager setMute:YES];

} else {

NSLog(@"进入失败");

[self reserveMeetingWithRoomId:self.LiaoTianSId];

}

}];

}else{

[self canRecord];

}

}];

}

}

- (void)reserveMeetingWithRoomId:(NSString *)roomId{

NIMNetCallMeeting *meeting = [[NIMNetCallMeeting alloc] init];

meeting.name = roomId;

meeting.type = NIMNetCallMediaTypeAudio;

meeting.actor = NO;

[[NIMAVChatSDK sharedSDK].netCallManager reserveMeeting:meeting completion:^(NIMNetCallMeeting * _Nonnull meeting, NSError * _Nonnull error) {

if (!error) {

NSLog(@"预定成功 ,进入房间");

[self joinNetCallWithRMeeting:meeting];

} else {

if (error.code == 417) {

[self joinNetCallWithRMeeting:meeting];

NSLog(@"meeting exists, %@",meeting);

error = nil;

} else {

NSLog(@"预定房间失败");

}

}

}];

}

2.3)用户加入会议

-(void)AddMeeting

{

NIMNetCallMeeting *meeting = [[NIMNetCallMeeting alloc] init];

meeting.name = 聊天室ID;

meeting.type = NIMNetCallMediaTypeAudio;

meeting.actor = NO;

[[FbwHttpClient shareInstance]startRequestMethod:POST parameters:@{@"userId":用户ID,@"chatRoomId":聊天室ID} url:UrL_JoinChatRoom success:^(id responseObject) {

if ([[responseObject objectForKey:@"errcode"]integerValue] != 0) {

[SVProgressHUD showErrorWithStatus:[responseObject objectForKey:@"msg"]];

}else{

//进入房间聊天

[[NIMAVChatSDK sharedSDK].netCallManager joinMeeting:meeting completion:^(NIMNetCallMeeting * _Nonnull meeting, NSError * _Nonnull error) {

if (!error) {

NSLog(@"进入成功");

[[NIMAVChatSDK sharedSDK].netCallManager setMute:YES];

[[NIMAVChatSDK sharedSDK].netCallManager setSpeaker:YES];//看需求是否开启扩音器

}else{

}

}];

}

} failure:^(NSError *error) {

}];

}

2.4)离开聊天室(踢出也如此)

[self quitConversationViewAndClear];

//                                    [self createOutRoom];

NIMNetCallMeeting *meeting = [[NIMNetCallMeeting alloc] init];

meeting.name = self.LiaoTianSId;

[[NIMAVChatSDK sharedSDK].netCallManager leaveMeeting:meeting];

2.5).主持人可以让用户上麦也可以让用户下麦以及旁听一看便知,很简单

网易互动直播聊天室(包含用户上麦下麦踢出聊天室)_第2张图片

2.6)到这里差不多也结束了,有几个注意的地方是你要区分主持人和用户,一抱麦用户即可说话,一下麦及不可,主持人一直可以,我们的需求你们可以看下图,我们聊天室用的融云,互动直播用的网易,当房间还有用户存在时,主持人退出聊天室还在,在麦用户还可以说话,当只有主持人一人时,退出聊天室,即关闭.

网易互动直播聊天室(包含用户上麦下麦踢出聊天室)_第3张图片

2.7)排版可能有点乱,看官们将就着看,做下来一开始确实有点麻烦,还好有位朋友指点就顺利多了,现在想想还挺简单的哈哈,互相学习,不断进步.

你可能感兴趣的:(网易互动直播聊天室(包含用户上麦下麦踢出聊天室))