04环信聊天界面 - 播放语音消息
2016-02-20 19:44
435 查看
分析:需要监听messageLabel的点击事件,然后播放
1.在chatCell里给messageLabel添加点击事件
1.在chatCell里给messageLabel添加点击事件
/** * 初始化 */ -(void)awakeFromNib { // 1.给messageLabel添加手势 self.messageLabel.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(messageLabelTap:)]; [self.messageLabel addGestureRecognizer: tap]; }
/** * 当用户点击messageLabel播放语言 */ - (void)messageLabelTap:(UIGestureRecognizer *)recognizer { id body = self.message.messageBodies[0]; if ([body isKindOfClass:[EMVoiceMessageBody class]]) { // 播放语音 BOOL receiver = [self.reuseIdentifier isEqualToString:recivierCell]; [AudioPlayTool playWithMessage:self.message messageLabel:self.messageLabel receiver:receiver]; } }2.播放语音的工具类
// // AudioPlayTool.h #import <UIKit/UIKit.h> @class EMMessage; @interface AudioPlayTool : NSObject /** * 播放语音 * * @param message EMMessage消息 * @param messageLabel 显示消息的label * @param receiver 是否是接收方 */ + (void)playWithMessage:(EMMessage *)message messageLabel:(UILabel *)messageLabel receiver:(BOOL)receiver; @end
// // AudioPlayTool.m // 环信项目 #import "AudioPlayTool.h" #import "EMCDDeviceManager.h" #import "EaseMob.h" /** * 正在执行动画的imageView */ static UIImageView *animatingImageView; @implementation AudioPlayTool + (void)playWithMessage:(EMMessage *)message messageLabel:(UILabel *)messageLabel receiver:(BOOL)receiver { // 0.把上个动画移除 [animatingImageView stopAnimating]; [animatingImageView removeFromSuperview]; // 1.播放语音 EMVoiceMessageBody *voiceBody = message.messageBodies[0]; NSString *path = voiceBody.localPath; NSFileManager *mgr = [NSFileManager defaultManager]; if ([mgr fileExistsAtPath:path] == NO) { // 如果本地语言文件不存在,使用服务器的 path = voiceBody.remotePath; } [[EMCDDeviceManager sharedInstance] asyncPlayingWithPath:path completion:^(NSError *error) { // 语言播放完成 // 移除动画 [animatingImageView stopAnimating]; [animatingImageView removeFromSuperview]; }]; // 2.添加动画 UIImageView *imgView = [[UIImageView alloc] init]; [messageLabel addSubview:imgView]; if (receiver) { imgView.frame = CGRectMake(0, 0, 20, 20); imgView.animationImages = @[ [UIImage imageNamed:@"chat_receiver_audio_playing000"], [UIImage imageNamed:@"chat_receiver_audio_playing001"], [UIImage imageNamed:@"chat_receiver_audio_playing002"], [UIImage imageNamed:@"chat_receiver_audio_playing003"] ]; }else{ imgView.frame = CGRectMake(messageLabel.bounds.size.width-20, 0, 20, 20); imgView.animationImages = @[ [UIImage imageNamed:@"chat_sender_audio_playing_000"], [UIImage imageNamed:@"chat_sender_audio_playing_001"], [UIImage imageNamed:@"chat_sender_audio_playing_002"], [UIImage imageNamed:@"chat_sender_audio_playing_003"] ]; } imgView.animationDuration = 1.0; [imgView startAnimating]; animatingImageView = imgView; } @end
相关文章推荐
- [破解实例][OllyDbg] CrackMe003-Afkayas.2
- CCIE学习笔记 2---BGP选路(属性值)
- 多文件上传 file-uploader.js
- Apache2.4+php5.5+Mysql5.5 Ubuntu14配置
- 一段代码
- HDU2539:点球大战
- 总结角色换装原理
- 基础练习 特殊的数字
- percona server安装
- Java-List测试-学生选课
- 【Codevs1215】迷宫
- Linux CentOS系统下菜单栏和任务消失的解决办法
- 基础练习 回文数
- Linux笔记(7)——压缩命令
- C++学习笔记之 构造函数的初始化列表
- leetcode——Reverse Integer
- js对象简单、深度克隆(复制)
- Unity3D教程:换装方法
- 写程序也好多年了,今天突然觉得应该不学到的东西记下来 同步/异步
- C++ Service中OpenFileMapping"拒绝访问"错误