| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- //
- // SoundCell.m
- // IBOSS
- //
- // Created by guan hong hou on 15/11/23.
- // Copyright © 2015年 elongtian. All rights reserved.
- //
- #import "SoundCell.h"
- #import "NSString+Tools.h"
- #define kTextFont [UIFont systemFontOfSize:14]
- @interface SoundCell(){
-
- }
- @end
- @implementation SoundCell
- #pragma mark -公有方法
- //初始化语音布局
- - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- return self;
-
- }
- #pragma mark -委托方法
- //语音完成播放回调方法
- - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)audioplayer successfully:(BOOL)flag {
-
- [_soundImageView stopAnimating];
- }
- #pragma mark -私有方法
- //声音播放事件
- - (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender
- {
- if(sender.numberOfTapsRequired == 1) {
- NSError * error;
- NSURL * url = [NSURL fileURLWithPath:_localPath];
-
- if (player) {
- [player stop];
- player = nil;
- }
- player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
- AVAudioSession *session = [AVAudioSession sharedInstance];
- [session setCategory:AVAudioSessionCategoryPlayback error:nil];
- [session setActive:YES error:nil];
- player.delegate=self;
- [player prepareToPlay];
- [player play];
-
- [_soundImageView startAnimating];
- }
- }
- //设置语音布局方法
- -(void)setPath:(NSString*)localPath:(NSString*)duration{
- _bubbleImage=[[UIImage imageNamed:@"wechat"]stretchableImageWithLeftCapWidth:18 topCapHeight:10];
- NSInteger durationTime=[duration integerValue];
- double bubbleWidth=50+durationTime;
-
- _bubbleView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 5,bubbleWidth,40)];
- [_bubbleView setUserInteractionEnabled:YES];
- [_bubbleView setImage:_bubbleImage];
- [self.contentView addSubview:_bubbleView];
- _soundImage =[UIImage imageNamed:@"receivervoicenodeplaying"];
- _soundImageView=[[UIImageView alloc] initWithFrame:CGRectMake(15,10,20, 20)];
-
- NSMutableArray *images = [NSMutableArray arrayWithCapacity:4];
- for (NSInteger i = 0; i < 4; i ++) {
- UIImage *image = [UIImage imageNamed:[@"receiver" stringByAppendingFormat:@"voicenodeplaying00%ld", (long)i]];
- if (image)
- [images addObject:image];
- }
-
- _soundImageView.animationImages = images;
- _soundImageView.animationDuration = 1.0;
- [_soundImageView stopAnimating];
- [_soundImageView setImage:_soundImage];
- [_bubbleView addSubview:_soundImageView];
- UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)];
- singleFingerOne.numberOfTouchesRequired = 1; //手指数
- singleFingerOne.numberOfTapsRequired = 1; //tap次数
- singleFingerOne.delegate= self;
- [_bubbleView addGestureRecognizer:singleFingerOne];
- _localPath=localPath;
- _duration=duration;
- NSString *s = [NSString stringWithFormat:@"%@%@",_duration,@"''"];;
- CGFloat x = CGRectGetMaxX(_bubbleView.frame)+3;
- CGFloat y = _bubbleView.frame.origin.y+12 ;
- _soundTime = [[UILabel alloc] initWithFrame:CGRectMake(x, y,100,20)];
- _soundTime.backgroundColor = [UIColor clearColor];
- _soundTime.textColor = LabelGrayTextColor;
- _soundTime.font=kTextFont;
- _soundTime.textAlignment = NSTextAlignmentLeft;
- [self.contentView addSubview:_soundTime];
- _soundTime.text=s;
- }
- @end
|