CWAudioPlayView.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // CWAudioPlayView.m
  3. // QQVoiceDemo
  4. //
  5. // Created by 陈旺 on 2017/10/4.
  6. // Copyright © 2017年 陈旺. All rights reserved.
  7. //
  8. #import "CWAudioPlayView.h"
  9. #import "UIView+CWChat.h"
  10. #import "CWRecordStateView.h"
  11. #import "CWAudioPlayer.h"
  12. #import "CWRecordModel.h"
  13. #import "CWRecorder.h"
  14. #import "CWVoiceView.h"
  15. @interface CWAudioPlayView ()
  16. @property (nonatomic, weak) CWRecordStateView *stateView;
  17. @property (nonatomic, weak) UIButton *playButton; // 播放按钮
  18. @property (nonatomic, weak) UIButton *cancelButton; // 取消按钮
  19. @property (nonatomic, weak) UIButton *sendButton; // 发送按钮
  20. @end
  21. @implementation CWAudioPlayView
  22. - (instancetype)initWithFrame:(CGRect)frame
  23. {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. // _progressValue = 0.8;
  27. [self setupSubViews];
  28. }
  29. return self;
  30. }
  31. - (void)setupSubViews {
  32. self.backgroundColor = [UIColor whiteColor];
  33. [self stateView];
  34. [self playButton];
  35. [self setupSendButtonAndCancelButton];
  36. [self listenProgress]; // 监听进度
  37. }
  38. #pragma mark - subviews
  39. - (UIButton *)playButton {
  40. if (_playButton == nil) {
  41. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  42. [btn setImage:[UIImage imageNamed:@"aio_record_play_nor"] forState:UIControlStateNormal];
  43. [btn setImage:[UIImage imageNamed:@"aio_record_play_press"] forState:UIControlStateHighlighted];
  44. [btn setImage:[UIImage imageNamed:@"aio_record_stop_nor"] forState:UIControlStateSelected];
  45. UIImage *image = [UIImage imageNamed:@"aio_voice_button_nor"];
  46. btn.frame = CGRectMake(0, 0, image.size.width, image.size.height);
  47. btn.center = CGPointMake(self.center.x, self.stateView.cw_bottom + image.size.width / 2);
  48. [btn addTarget:self action:@selector(playRecord) forControlEvents:UIControlEventTouchUpInside];
  49. [self addSubview:btn];
  50. _playButton = btn;
  51. }
  52. return _playButton;
  53. }
  54. - (CWRecordStateView *)stateView {
  55. if (_stateView == nil) {
  56. CWRecordStateView *stateView = [[CWRecordStateView alloc] initWithFrame:CGRectMake(0, 10, self.cw_width, 50)];
  57. [self addSubview:stateView];
  58. stateView.recordState = CWRecordStatePreparePlay;
  59. _stateView = stateView;
  60. }
  61. return _stateView;
  62. }
  63. - (void)setupSendButtonAndCancelButton {
  64. CGFloat height = 40;
  65. UIButton *cancelBtn = [self buttonWithFrame:CGRectMake(0, self.cw_height - height, self.cw_width / 2.0, height) title:ASLocalizedString(@"取消")titleColor:kSelectBackGroudColor font:[UIFont systemFontOfSize:18] backImageNor:@"aio_record_cancel_button" backImageHighled:@"aio_record_cancel_button_press" sel:@selector(btnClick:)];
  66. [self addSubview:cancelBtn];
  67. self.cancelButton = cancelBtn;
  68. UIButton *sendBtn = [self buttonWithFrame:CGRectMake(self.cw_width / 2.0, self.cw_height - height, self.cw_width / 2.0, height) title:ASLocalizedString(@"发送")titleColor:kSelectBackGroudColor font:[UIFont systemFontOfSize:18] backImageNor:@"aio_record_send_button" backImageHighled:@"aio_record_send_button_press" sel:@selector(btnClick:)];
  69. [self addSubview:sendBtn];
  70. self.sendButton = sendBtn;
  71. }
  72. - (UIButton *)buttonWithFrame:(CGRect)frame title:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font backImageNor:(NSString *)backImageNor backImageHighled:(NSString *)backImageHighled sel:(SEL)sel{
  73. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  74. btn.frame = frame;
  75. [btn setTitle:title forState:UIControlStateNormal];
  76. [btn setTitleColor:titleColor forState:UIControlStateNormal];
  77. btn.titleLabel.font = font;
  78. UIImage *newImageNor = [[UIImage imageNamed:backImageNor] stretchableImageWithLeftCapWidth:2 topCapHeight:2];
  79. UIImage *newImageHighled = [[UIImage imageNamed:backImageHighled] stretchableImageWithLeftCapWidth:2 topCapHeight:2];
  80. [btn setBackgroundImage:newImageNor forState:UIControlStateNormal];
  81. [btn setBackgroundImage:newImageHighled forState:UIControlStateHighlighted];
  82. [btn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
  83. return btn;
  84. }
  85. #pragma mark - play/stop
  86. - (void)playRecord {
  87. self.playButton.selected = !self.playButton.selected;
  88. if (self.playButton.selected) {
  89. self.stateView.recordState = CWRecordStatePlay;
  90. [[CWAudioPlayer shareInstance] playAudioWith:[CWRecordModel shareInstance].path];
  91. }else {
  92. [self stopPlay];
  93. }
  94. }
  95. - (void)stopPlay {
  96. self.playButton.selected = NO;
  97. self.stateView.recordState = CWRecordStatePreparePlay;
  98. [[CWAudioPlayer shareInstance] stopCurrentAudio];
  99. _progressValue = 0;
  100. [self setNeedsDisplay];
  101. [self layoutIfNeeded];
  102. }
  103. - (void)btnClick:(UIButton *)btn {
  104. // NSLog(@"%@",btn.titleLabel.text);
  105. [self stopPlay];
  106. if (btn == self.sendButton) { // 发送
  107. NSLog(ASLocalizedString(@"发送...path: %@"),[CWRecordModel shareInstance].path);
  108. KPostNotification(KRecordingEnd, @{});
  109. }else {
  110. NSLog(ASLocalizedString(@"取消发送并删除录音"));
  111. [[CWRecorder shareInstance] deleteRecord];
  112. }
  113. [(CWVoiceView *)self.superview.superview.superview setState:CWVoiceStateDefault];
  114. [self removeFromSuperview];
  115. }
  116. #pragma mark 监听环形进度条更新
  117. - (void)listenProgress {
  118. __weak typeof(self) weakSelf = self;
  119. self.stateView.playProgress = ^(CGFloat progress) {
  120. if (progress == 1) {
  121. progress = 0;
  122. [weakSelf stopPlay];
  123. }
  124. _progressValue = progress;
  125. [weakSelf setNeedsDisplay];
  126. [weakSelf layoutIfNeeded];
  127. };
  128. }
  129. - (void)drawRect:(CGRect)rect {
  130. [super drawRect:rect];
  131. UIImage *image = [UIImage imageNamed:@"aio_voice_button_nor"];
  132. CGContextRef ctx = UIGraphicsGetCurrentContext();
  133. CGContextSetLineWidth(ctx, 2.0f);
  134. CGContextSetStrokeColorWithColor(ctx, [UIColorFromRGBA(214, 219, 222, 1.0) CGColor]);
  135. CGContextAddArc(ctx, self.center.x, self.stateView.cw_bottom + image.size.width / 2, image.size.width / 2, 0, M_PI * 2, 0);
  136. CGContextStrokePath(ctx);
  137. CGContextSetStrokeColorWithColor(ctx, [kSelectBackGroudColor CGColor]);
  138. CGFloat startAngle = -M_PI_2;
  139. CGFloat angle = self.progressValue * M_PI * 2;
  140. CGFloat endAngle = startAngle + angle;
  141. CGContextAddArc(ctx, self.center.x, self.stateView.cw_bottom + image.size.width / 2, image.size.width / 2, startAngle, endAngle, 0);
  142. CGContextStrokePath(ctx);
  143. }
  144. @end