@@ -59,12 +59,12 @@ - (void)viewDidLoad
5959// [self setBackgroundImage:[UIImage imageNamed:@"TableViewBackgroundImage"]];
6060
6161 // 设置自身用户名
62- self.messageSender = kDarcyClientID ;
62+ self.messageSender = [[LeanChatManager manager ] selfClientID ] ;
6363
6464 // 添加第三方接入数据
6565 NSMutableArray *shareMenuItems = [NSMutableArray array ];
66- NSArray *plugIcons = @[@" sharemore_pic" , @" sharemore_video" , @" sharemore_location" , @" sharemore_friendcard " , @" sharemore_myfav " , @" sharemore_wxtalk " , @" sharemore_videovoip " , @" sharemore_voiceinput" , @" sharemore_openapi" , @" sharemore_openapi" , @" avatar " ];
67- NSArray *plugTitle = @[@" 照片" , @" 拍摄" , @" 位置" , @" 名片 " , @" 我的收藏 " , @" 实时对讲机 " , @" 视频聊天 " , @" 语音输入" , @" 大众点评" , @" 应用" , @" 曾宪华" ];
66+ NSArray *plugIcons = @[@" sharemore_pic" , @" sharemore_video" ,@" sharemore_location" , @" sharemore_videovoip " , @" sharemore_friendcard " , @" sharemore_myfav " , @" sharemore_wxtalk " , @" sharemore_voiceinput" , @" sharemore_openapi" , @" sharemore_openapi" , @" Avatar " ];
67+ NSArray *plugTitle = @[@" 照片" , @" 拍摄" ,@" 位置" ,@" 视频 " , @" 名片 " , @" 我的收藏 " , @" 实时对讲机 " , @" 语音输入" , @" 大众点评" , @" 应用" , @" 曾宪华" ];
6868 for (NSString *plugIcon in plugIcons) {
6969 XHShareMenuItem *shareMenuItem = [[XHShareMenuItem alloc ] initWithNormalIconImage: [UIImage imageNamed: plugIcon] title: [plugTitle objectAtIndex: [plugIcons indexOfObject: plugIcon]]];
7070 [shareMenuItems addObject: shareMenuItem];
@@ -78,7 +78,7 @@ - (void)viewDidLoad
7878 for (NSInteger j = 0 ; j < 18 ; j ++) {
7979 XHEmotion *emotion = [[XHEmotion alloc ] init ];
8080 NSString *imageName = [NSString stringWithFormat: @" section%ld _emotion%ld " , (long )i , (long )j % 16 ];
81- emotion.emotionPath = [[NSBundle mainBundle ] pathForResource: [NSString stringWithFormat: @" emotion%ld .gif " , (long )j ] ofType: @" " ];
81+ emotion.emotionPath = [[NSBundle mainBundle ] pathForResource: [NSString stringWithFormat: @" emotion%ld " , (long )(j% 16 ) ] ofType: @" gif " ];
8282 emotion.emotionConverPhoto = [UIImage imageNamed: imageName];
8383 [emotions addObject: emotion];
8484 }
@@ -103,7 +103,7 @@ - (void)viewDidLoad
103103 // 设置接收消息
104104 [[LeanChatManager manager ] setupDidReceiveCommonMessageCompletion: ^(AVIMMessage *message) {
105105 // 普通信息
106- [self insertAVIMTypedMessage: message];
106+ // [self insertAVIMTypedMessage:message];
107107 }];
108108 [[LeanChatManager manager ] setupDidReceiveTypedMessageCompletion: ^(AVIMTypedMessage *message) {
109109 // 富文本信息
@@ -150,6 +150,43 @@ -(void)sendVoiceMessage:(XHMessage*)voiceMessage{;
150150 }];
151151}
152152
153+ -(void )sendEmotionMessage : (XHMessage*)emotionMessage {
154+ AVFile* file=[AVFile fileWithName: @" emotion" contentsAtPath: emotionMessage.emotionPath];
155+ WEAKSELF
156+ [file saveInBackgroundWithBlock: ^(BOOL succeeded, NSError *error) {
157+ if (error==nil ){
158+ AVIMEmotionMessage* sendEmotionMessage=[AVIMEmotionMessage messageWithText: file.url attributes: nil ];
159+ [weakSelf.conversation sendMessage: sendEmotionMessage callback: ^(BOOL succeeded, NSError *error) {
160+ DLog (@" succeed: %d , error:%@ " ,succeeded,error);
161+ }];
162+ }else {
163+ DLog (@" error:%@ " ,error);
164+ }
165+ }];
166+ }
167+
168+ -(void )sendVideoMessage : (XHMessage*)videoMessage {
169+ AVIMVideoMessage* sendVideoMessage=[AVIMVideoMessage messageWithText: nil attachedFilePath: videoMessage.videoPath attributes: nil ];
170+ [self .conversation sendMessage: sendVideoMessage callback: ^(BOOL succeeded, NSError *error) {
171+ DLog (@" succeed: %d , error:%@ " ,succeeded,error);
172+ }];
173+ }
174+
175+ typedef void (^FetchFileBlock)(NSString * path,NSError * error);
176+
177+ -(void )fetchDataOfMessageFile : (AVFile*)file messageId : (NSString *)messageId completion : (FetchFileBlock)completion {
178+ NSString * path=[[NSSearchPathForDirectoriesInDomains (NSDocumentationDirectory, NSUserDomainMask, YES ) firstObject ] stringByAppendingString: messageId];
179+ [file getDataInBackgroundWithBlock: ^(NSData *data, NSError *error) {
180+ if (error){
181+ DLog (@" %@ " ,error);
182+ completion (nil ,error);
183+ }else {
184+ [data writeToFile: path atomically: YES ];
185+ completion (path,error);
186+ }
187+ }];
188+ }
189+
153190- (void )insertAVIMTypedMessage : (AVIMTypedMessage *)receiveMessage {
154191 AVIMMessageMediaType msgType = receiveMessage.mediaType ;
155192 switch (msgType) {
@@ -176,25 +213,48 @@ - (void)insertAVIMTypedMessage:(AVIMTypedMessage *)receiveMessage {
176213 break ;
177214 }
178215 case kAVIMMessageMediaTypeAudio :{
179- AVIMAudioMessage* audioMessage=(AVIMAudioMessage*)receiveMessage;;
180- NSDate * timestamp=[NSDate dateWithTimeIntervalSince1970: receiveMessage.sendTimestamp/1000 ];
181- NSString * voicePath=[[NSSearchPathForDirectoriesInDomains (NSDocumentationDirectory, NSUserDomainMask, YES ) firstObject ] stringByAppendingString: audioMessage.messageId];
182- XHMessage* voiceMessage=[[XHMessage alloc ] initWithVoicePath: voicePath voiceUrl: audioMessage.file.url voiceDuration: [NSString stringWithFormat: @" %.1f " ,audioMessage.duration] sender: audioMessage.clientId timestamp: timestamp];
183- voiceMessage.bubbleMessageType = XHBubbleMessageTypeReceiving;
184- voiceMessage.avatar = [UIImage imageNamed: @" Avatar" ];
185- voiceMessage.avatarUrl = @" http://www.pailixiu.com/jack/meIcon@2x.png" ;
186- [self addMessage: voiceMessage];
187- [self finishSendMessageWithBubbleMessageType: XHBubbleMessageMediaTypeVoice];
188- NSFileManager * fileManager=[NSFileManager defaultManager ];
189- if ([fileManager fileExistsAtPath: voicePath]==NO ){
190- [audioMessage.file getDataInBackgroundWithBlock: ^(NSData *data, NSError *error) {
191- if (error==nil ){
192- [data writeToFile: voicePath atomically: YES ];
193- }else {
194- DLog (@" error:%@ " ,error);
195- }
196- }];
197- }
216+ WEAKSELF ;
217+ [self fetchDataOfMessageFile: receiveMessage.file messageId: receiveMessage.messageId completion: ^(NSString *path, NSError *error) {
218+ NSDate * timestamp=[NSDate dateWithTimeIntervalSince1970: receiveMessage.sendTimestamp/1000 ];
219+ AVIMAudioMessage* audioMessage=(AVIMAudioMessage*)receiveMessage;
220+ XHMessage* voiceMessage=[[XHMessage alloc ] initWithVoicePath: path voiceUrl: nil voiceDuration: [NSString stringWithFormat: @" %.1f " ,audioMessage.duration] sender: receiveMessage.clientId timestamp: timestamp];
221+ voiceMessage.bubbleMessageType = XHBubbleMessageTypeReceiving;
222+ voiceMessage.avatar = [UIImage imageNamed: @" Avatar" ];
223+ voiceMessage.avatarUrl = @" http://www.pailixiu.com/jack/meIcon@2x.png" ;
224+ [weakSelf addMessage: voiceMessage];
225+ [weakSelf finishSendMessageWithBubbleMessageType: XHBubbleMessageMediaTypeVoice];
226+ }];
227+ break ;
228+ }
229+ case kAVIMMessageMediaTypeEmotion :{
230+ WEAKSELF
231+ AVFile* file=[AVFile fileWithURL: receiveMessage.text];
232+ [self fetchDataOfMessageFile: file messageId: receiveMessage.messageId completion: ^(NSString *path, NSError *error) {
233+ NSDate * timestamp=[NSDate dateWithTimeIntervalSince1970: receiveMessage.sendTimestamp/1000 ];
234+ XHMessage *emotionMessage = [[XHMessage alloc ] initWithEmotionPath: path sender: receiveMessage.clientId timestamp: timestamp];
235+ emotionMessage.bubbleMessageType = XHBubbleMessageTypeReceiving;
236+ emotionMessage.avatar = [UIImage imageNamed: @" Avatar" ];
237+ emotionMessage.avatarUrl = @" http://www.pailixiu.com/jack/meIcon@2x.png" ;
238+ [weakSelf addMessage: emotionMessage];
239+ [weakSelf finishSendMessageWithBubbleMessageType: XHBubbleMessageMediaTypeEmotion];
240+ }];
241+ break ;
242+ }
243+ case kAVIMMessageMediaTypeVideo :{
244+ WEAKSELF
245+ [self fetchDataOfMessageFile: receiveMessage.file messageId: receiveMessage.messageId completion: ^(NSString *path, NSError *error) {
246+ UIImage *thumbnailImage;
247+ if (!error){
248+ thumbnailImage= [XHMessageVideoConverPhotoFactory videoConverPhotoWithVideoPath: path];
249+ }
250+ NSDate * timestamp=[NSDate dateWithTimeIntervalSince1970: receiveMessage.sendTimestamp/1000 ];
251+ XHMessage *videoMessage = [[XHMessage alloc ] initWithVideoConverPhoto: thumbnailImage videoPath: path videoUrl: nil sender: receiveMessage.clientId timestamp: timestamp];
252+ videoMessage.bubbleMessageType = XHBubbleMessageTypeReceiving;
253+ videoMessage.avatar = [UIImage imageNamed: @" Avatar" ];
254+ videoMessage.avatarUrl = @" http://www.pailixiu.com/jack/meIcon@2x.png" ;
255+ [weakSelf addMessage: videoMessage];
256+ [weakSelf finishSendMessageWithBubbleMessageType: XHBubbleMessageMediaTypeVideo];
257+ }];
198258 break ;
199259 }
200260 default :
@@ -367,6 +427,7 @@ - (void)didSendVideoConverPhoto:(UIImage *)videoConverPhoto videoPath:(NSString
367427 videoMessage.avatarUrl = @" http://www.pailixiu.com/jack/meIcon@2x.png" ;
368428 [self addMessage: videoMessage];
369429 [self finishSendMessageWithBubbleMessageType: XHBubbleMessageMediaTypeVideo];
430+ [self sendVideoMessage: videoMessage];
370431}
371432
372433/* *
@@ -399,6 +460,7 @@ - (void)didSendEmotion:(NSString *)emotionPath fromSender:(NSString *)sender onD
399460 emotionMessage.avatarUrl = @" http://www.pailixiu.com/jack/meIcon@2x.png" ;
400461 [self addMessage: emotionMessage];
401462 [self finishSendMessageWithBubbleMessageType: XHBubbleMessageMediaTypeEmotion];
463+ [self sendEmotionMessage: emotionMessage];
402464}
403465
404466/* *
0 commit comments