SoundCell.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // SoundCell.m
  3. // IBOSS
  4. //
  5. // Created by guan hong hou on 15/11/23.
  6. // Copyright © 2015年 elongtian. All rights reserved.
  7. //
  8. #import "SoundCell.h"
  9. #import "NSString+Tools.h"
  10. #define kTextFont [UIFont systemFontOfSize:14]
  11. @interface SoundCell(){
  12. }
  13. @end
  14. @implementation SoundCell
  15. #pragma mark -公有方法
  16. //初始化语音布局
  17. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  18. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  19. return self;
  20. }
  21. #pragma mark -委托方法
  22. //语音完成播放回调方法
  23. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)audioplayer successfully:(BOOL)flag {
  24. [_soundImageView stopAnimating];
  25. }
  26. #pragma mark -私有方法
  27. //声音播放事件
  28. - (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender
  29. {
  30. if(sender.numberOfTapsRequired == 1) {
  31. NSError * error;
  32. NSURL * url = [NSURL fileURLWithPath:_localPath];
  33. if (player) {
  34. [player stop];
  35. player = nil;
  36. }
  37. player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
  38. AVAudioSession *session = [AVAudioSession sharedInstance];
  39. [session setCategory:AVAudioSessionCategoryPlayback error:nil];
  40. [session setActive:YES error:nil];
  41. player.delegate=self;
  42. [player prepareToPlay];
  43. [player play];
  44. [_soundImageView startAnimating];
  45. }
  46. }
  47. //设置语音布局方法
  48. -(void)setPath:(NSString*)localPath:(NSString*)duration{
  49. _bubbleImage=[[UIImage imageNamed:@"wechat"]stretchableImageWithLeftCapWidth:18 topCapHeight:10];
  50. NSInteger durationTime=[duration integerValue];
  51. double bubbleWidth=50+durationTime;
  52. _bubbleView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 5,bubbleWidth,40)];
  53. [_bubbleView setUserInteractionEnabled:YES];
  54. [_bubbleView setImage:_bubbleImage];
  55. [self.contentView addSubview:_bubbleView];
  56. _soundImage =[UIImage imageNamed:@"receivervoicenodeplaying"];
  57. _soundImageView=[[UIImageView alloc] initWithFrame:CGRectMake(15,10,20, 20)];
  58. NSMutableArray *images = [NSMutableArray arrayWithCapacity:4];
  59. for (NSInteger i = 0; i < 4; i ++) {
  60. UIImage *image = [UIImage imageNamed:[@"receiver" stringByAppendingFormat:@"voicenodeplaying00%ld", (long)i]];
  61. if (image)
  62. [images addObject:image];
  63. }
  64. _soundImageView.animationImages = images;
  65. _soundImageView.animationDuration = 1.0;
  66. [_soundImageView stopAnimating];
  67. [_soundImageView setImage:_soundImage];
  68. [_bubbleView addSubview:_soundImageView];
  69. UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)];
  70. singleFingerOne.numberOfTouchesRequired = 1; //手指数
  71. singleFingerOne.numberOfTapsRequired = 1; //tap次数
  72. singleFingerOne.delegate= self;
  73. [_bubbleView addGestureRecognizer:singleFingerOne];
  74. _localPath=localPath;
  75. _duration=duration;
  76. NSString *s = [NSString stringWithFormat:@"%@%@",_duration,@"''"];;
  77. CGFloat x = CGRectGetMaxX(_bubbleView.frame)+3;
  78. CGFloat y = _bubbleView.frame.origin.y+12 ;
  79. _soundTime = [[UILabel alloc] initWithFrame:CGRectMake(x, y,100,20)];
  80. _soundTime.backgroundColor = [UIColor clearColor];
  81. _soundTime.textColor = LabelGrayTextColor;
  82. _soundTime.font=kTextFont;
  83. _soundTime.textAlignment = NSTextAlignmentLeft;
  84. [self.contentView addSubview:_soundTime];
  85. _soundTime.text=s;
  86. }
  87. @end