做环信聊天室遇到的坑

1.加入聊天室 修改EaseUI提供的界面
(1)发送人接收人的消息都在左侧:
a.自定义展示消息的cell集成自环信的EaseBaseMessageCell,复写父类的约束视图方法,修改各个视图的约束
b.复写父类的计算单元格高度的方法,重新计算单元格的高度

//去掉会话的气泡背景 [[EaseBaseMessageCell appearance] setSendBubbleBackgroundImage:[[UIImage imageNamed:@""] stretchableImageWithLeftCapWidth:35 topCapHeight:35]]; [[EaseBaseMessageCell appearance] setRecvBubbleBackgroundImage:[[UIImage imageNamed:@""] stretchableImageWithLeftCapWidth:35 topCapHeight:35]]; //设置接收气泡 chat_receiver_bg//显示表情 EaseEmotionManager * manager = [[EaseEmotionManager alloc] initWithType:EMEmotionDefault emotionRow:3 emotionCol:7 emotions:[EaseEmoji allEmoji]]; [self.faceView setEmotionManagers:@[manager]];

2.给聊天室的消息发送者添加昵称
在环信的EaseUIZ中的 EaseMessageViewController 把添加昵称写在这个方法中 - (void)_sendMessage:(EMMessage *)message{ NSString *nickName = [[LKUserInfoManager sharedUserInfo] readUserInfo].userinfo.nickname; NSString *userid = [[LKUserInfoManager sharedUserInfo] readUserInfo].userinfo.sysUserid; NSDictionary *dic = @{@"Name":nickName, @"UserId":userid}; NSArray *arr = @[dic]; NSDictionary *ext = @{@"extmsg":arr}; message.ext = ext; }

3.断网再连接网络消息发不出去
//监听环信的链接成功,若退出聊天室重新加入聊天室(考虑切换网络状态的情况) [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(huanxinConnectionSuccess:) name:@"HuanxinConnectionSuccessNotification" object:nil];

4.聊天室的一点说明
(1)在聊天室时可以接收到聊天室消息
(2)不退出聊天室,app在后台环信在线状态可以收到消息
(3)退出聊天室无论环信在线还是离线,app在前台还是后台都不能收到聊天室消息了
5.环信语音与其他sdk的冲突(自己项目中的sdk方头的温度计sdk,乐视标准直播sdk)
(1)温度计用到了音视频,测量温度是需要在播放录制状态下(AVAudioSessionCategoryPlayAndRecord),会与环信发送语音冲突,环信录制时修改为录制状态(AVAudioSessionCategoryRecord),播放时为播放状态(AVAudioSessionCategoryPlayback)
解决办法:测温时改变状态
//在听筒状态下可以测温(打开听筒:用于既需要播放声音又需要录音的应用,语音聊天应用(如微信)应该使用这个category。该Category提供录音和播放功能,声音的默认出口为听筒) AVAudioSession *session = [AVAudioSession sharedInstance]; NSArray *categrorys = [session availableCategories]; if ([categrorys containsObject:AVAudioSessionCategoryPlayAndRecord]) { if (![[session category] isEqualToString:AVAudioSessionCategoryPlayAndRecord]) { NSError *sessionError; // NSError *error; //[session setActive:YES error:&error]; [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError]; } }

【做环信聊天室遇到的坑】(2)在直播界面用到了乐视直播sdk,环信聊天室。直播要在播放状态,聊天室语音发送修改为录制状态,语音播放是改为播放状态,所以发语音时直播暂停了
解决办法:
EMCDDeviceManagerBase.m文件中修改环信发语音修改的状态
switch (session) { case EM_AUDIOPLAYER: audioSessionCategory = AVAudioSessionCategoryPlayback; break; case EM_AUDIORECORDER: audioSessionCategory = AVAudioSessionCategoryPlayAndRecord; //AVAudioSessionCategoryRecord; break; default: audioSessionCategory = AVAudioSessionCategoryPlayback; //AVAudioSessionCategoryAmbient; break; } //zhx modify(解决与测温的冲突,时而发送语音失败)注释掉以下代码 //if (isNeedActive) { //BOOL success = [audioSession setActive:isActive //withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation //error:&error]; //if(!success || error){ //error = [NSError errorWithDomain:NSEaseLocalizedString(@"error.initPlayerFail", @"Failed to initialize AVAudioPlayer") //code:-1 //userInfo:nil]; //return error; //} //}setActive:YES与setActive:NO一般要成对出现。 没有setActive:YES然后就setActive:NO会抛异常:AVAudioSession.mm:623: -[AVAudioSession setActive:withOptions:error:]: Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.

    推荐阅读