因为项目要求,花了快一个月的时间都在做环信的即时聊天,中间也是查查找找,看看别人的代码,看看环信的文档,所以现在打算整合一份给大家参考。
项目中只做了文字,语音,位置,图片发送。看这篇文章的时候默认读者已经阅读了解环信3.0文档。
以下代码都是封装好的,可以直接调用
发送文字
/**
* @param txtStr 消息文字
* @param chatBossTel 接收方
*/
#pragma mark 环信-发送文字动作
-(void)sendMessage:(NSString *)txtStr
{
//加入子线程,不会造成卡顿
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//生成消息
EMTextMessageBody *body=[[EMTextMessageBody alloc]initWithText:txtStr];
//消息发送方
NSString *from=[[EMClient sharedClient]currentUsername];
//消息的接收方
EMMessage *message=[[EMMessage alloc]initWithConversationID:self.chatBossTel from:from to:self.chatBossTel body:body ext:nil];
//消息类型是单聊
message.chatType=EMChatTypeChat;
//发送消息
[[EMClient sharedClient].chatManager asyncSendMessage:message progress:^(int progress) {
} completion:^(EMMessage *message, EMError *error) {
CLog(@"发送的消息错误原因%@",error);
if (!error)
{
//把消息加入数组
[self.messArr addObject:message];
//计算高度
[self.messHeightArr addObjectsFromArray:[self calculateMesseageHeightWithCell:@[message]]];
//滑动到最后一行
dispatch_async(dispatch_get_main_queue(), ^{
[self tableViewScrollToBottom];
});
}
}];
});
}
发送语音
#pragma mark环信-发送语音
/**
* @param localPath 语音地址
* @param duration 语音秒数
*/
-(void)sendVoiceWithLocalPath:(NSString*)localPath
andDisplayName:(NSString*)displayName
andDuration:(int)duration
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//生成语音
EMVoiceMessageBody *body=[[EMVoiceMessageBody alloc]initWithLocalPath:localPath displayName:displayName];
//语音的秒数
body.duration=self.recordV.duration;
NSString *from=[[EMClient sharedClient]currentUsername];
//生成Mes
EMMessage *message=[[EMMessage alloc]initWithConversationID:self.chatBossTel from:from to:self.chatBossTel body:body ext:nil];
//设为单聊
message.chatType=EMChatTypeChat;
if (duration>1)//大于1秒
{
[[EMClient sharedClient].chatManager asyncSendMessage:message progress:^(int progress) {
} completion:^(EMMessage *message, EMError *error) {
//加入tab
[self.messArr addObject:message];
[self.messHeightArr addObjectsFromArray:[self calculateMesseageHeightWithCell:@[message]]];
dispatch_async(dispatch_get_main_queue(), ^{
[self tableViewScrollToBottom];
});
}];
}
else
{
//停止录音
[self.recordV stopRecordVocie];
//删除录音
[self.recordV deleteVoice];
dispatch_async(dispatch_get_main_queue(), ^{
//录音时间太短请重新
[WarnWindow HUD:self.view andWarnText:@"录音时间太短请重新" andXoffset:0 andYoffset:0];
});
}
});
}
发送位置
#pragma mark 环信-发送位置
/**
* @param latitude 纬度
* @param longitude 经度
* @param location 位置
*/
-(void)sendLocationWithLatitude:(CGFloat)latitude
andLongitude:(CGFloat)longitude
andLocation:(NSString*)location
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//生成位置
EMLocationMessageBody *body=[[EMLocationMessageBody alloc]initWithLatitude:latitude longitude:longitude address:location];
NSString *from=[[EMClient sharedClient]currentUsername];
//生成Mes
EMMessage *message=[[EMMessage alloc]initWithConversationID:self.chatBossTel from:from to:self.chatBossTel body:body ext:nil];
//设为单聊
message.chatType=EMChatTypeChat;
//发送语音
[[EMClient sharedClient].chatManager asyncSendMessage:message progress:^(int progress) {
} completion:^(EMMessage *message, EMError *error) {
CLog(@"发送位置的错误%@",error);
if (!error)
{
//加入tab
[self.messArr addObject:message];
//计算高度
[self.messHeightArr addObjectsFromArray:[self calculateMesseageHeightWithCell:@[message]]];
dispatch_async(dispatch_get_main_queue(), ^{
[self tableViewScrollToBottom];
});
}
}];
});
}
发送图片
#pragma mark 相册代理 发送图片
//从相册中获取图片进行发送,正在发送时带了一个菊花
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//从字典中获取原始对象
UIImage *imgV=info[UIImagePickerControllerOriginalImage];
NSData *data=UIImageJPEGRepresentation(imgV, 0.5);
//生成图片的data
EMImageMessageBody *body=[[EMImageMessageBody alloc]initWithData:data displayName:@"image.png"];
NSString *from=[[EMClient sharedClient] currentUsername];
EMMessage *message=[[EMMessage alloc]initWithConversationID:self.chatBossTel from:from to:self.chatBossTel body:body ext:nil];
message.chatType=EMChatTypeChat;
[self creatHud];//显示菊花
//发送图片
[[EMClient sharedClient].chatManager asyncSendMessage:message progress:^(int progress) {
if (progress==100)
{
[hud hide:YES];//隐藏菊花
}
} completion:^(EMMessage *message, EMError *error) {
CLog(@"发送图片Error%@",error);
if (!error)
{ //存入数组
[self.messArr addObject:message];
[self.messHeightArr addObjectsFromArray:[self calculateMesseageHeightWithCell:@[message]]];
[self tableViewScrollToBottom];
}
}];
[picker dismissViewControllerAnimated:YES completion:nil];//模态视图
}
网友评论
NSString* fileDirectory = [[[directory stringByAppendingPathComponent:fileName]
stringByAppendingPathExtension:type]
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
CLog(@"文件路径%@",fileDirectory);
return fileDirectory;
在播放录音的时候获取录音的音量,代理返回。
[self.recorder updateMeters];//刷新音量数据
double lowPassResults=pow(10, (0.05 * [self.recorder peakPowerForChannel:0]));
CLog(@"%lf",lowPassResults);
[self.delegate sendDetectionVoice:lowPassResults];
#pragma mark 音量代理-改变音量视图
-(void)sendDetectionVoice:(double)lowPassResults
{
if (0<lowPassResults&&lowPassResults<=0.25)
{
self.recordStatusImgV.image=Image(@"sendAudio_1@2x");
}
else if(lowPassResults>0.25&&lowPassResults<=0.5)
{
self.recordStatusImgV.image=Image(@"sendAudio_2@2x");
}
else if (lowPassResults>0.5&&lowPassResults<=0.75)
{
self.recordStatusImgV.image=Image(@"sendAudio_3@2x");
}
else
{
self.recordStatusImgV.image=Image(@"sendAudio_4@2x");
}
}
这个image就是播放语音那三个弯曲线图片,或者弄一张gif图片也可以。
/var/mobile/Containers/Data/Application/4DB78406-26E0-4C0F-B996-135E7D07A25A/voiceFile505460825.wav not exist