CWVoiceChangePlayView.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // CWVoiceChangePlayView.m
  3. // QQVoiceDemo
  4. //
  5. // Created by chavez on 2017/10/11.
  6. // Copyright © 2017年 陈旺. All rights reserved.
  7. //
  8. #import "CWVoiceChangePlayView.h"
  9. #import "UIView+CWChat.h"
  10. #import "CWVoiceChangePlayCell.h"
  11. #import "CWAudioPlayer.h"
  12. #import "CWRecordModel.h"
  13. #import "CWVoiceView.h"
  14. #import "CWRecorder.h"
  15. #import "CWFlieManager.h"
  16. #import "HKPTimeLine.pch"
  17. @interface CWVoiceChangePlayView()
  18. @property (nonatomic, weak) UIButton *cancelButton; // 取消按钮
  19. @property (nonatomic, weak) UIButton *sendButton; // 发送按钮
  20. @property (nonatomic,strong) CADisplayLink *playTimer; // 播放时振幅计时器
  21. @property (nonatomic,weak) CWVoiceChangePlayCell *playingView;
  22. @property (nonatomic,strong) NSMutableArray *imageNames;
  23. @property (nonatomic,weak) UIScrollView *contentScrollView;
  24. @end
  25. @implementation CWVoiceChangePlayView
  26. #pragma mark - lazyLoad
  27. - (NSMutableArray *)imageNames {
  28. if (_imageNames == nil) {
  29. _imageNames = [NSMutableArray array];
  30. for (int i = 0; i < 6; i++) {
  31. [_imageNames addObject:[NSString stringWithFormat:@"aio_voiceChange_effect_%d",i]];
  32. }
  33. }
  34. return _imageNames;
  35. }
  36. - (instancetype)initWithFrame:(CGRect)frame
  37. {
  38. self = [super initWithFrame:frame];
  39. if (self) {
  40. [self setupSubviews];
  41. }
  42. return self;
  43. }
  44. - (void)setupSubviews {
  45. [self setupContentScrollView];
  46. [self setupSendButtonAndCancelButton];
  47. }
  48. #pragma mark - setupUI
  49. - (void)setupContentScrollView {
  50. UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
  51. scrollView.backgroundColor = [UIColor whiteColor];
  52. scrollView.bounces = YES;
  53. scrollView.cw_height = scrollView.cw_height - 40;
  54. scrollView.showsVerticalScrollIndicator = NO;
  55. [self addSubview:scrollView];
  56. self.contentScrollView = scrollView;
  57. NSArray *titles = @[ASLocalizedString(@"原声"),ASLocalizedString(@"萝莉"),ASLocalizedString(@"大叔"),ASLocalizedString(@"惊悚"),ASLocalizedString(@"空灵"),ASLocalizedString(@"搞怪")];
  58. CGFloat width = self.cw_width / 4;
  59. CGFloat height = width + 10;
  60. __weak typeof(self) weakSelf = self;
  61. for (int i = 0; i < self.imageNames.count; i++) {
  62. CWVoiceChangePlayCell *cell = [[CWVoiceChangePlayCell alloc] initWithFrame:CGRectMake(i%4 * width, i / 4 * height, width, height)];
  63. cell.center = scrollView.center;
  64. cell.imageName = self.imageNames[i];
  65. cell.title = titles[i];
  66. [self.contentScrollView addSubview:cell];
  67. [UIView animateWithDuration:0.25 animations:^{
  68. cell.frame = CGRectMake(i%4 * width, i / 4 * height, width, height);
  69. } completion:^(BOOL finished) {
  70. cell.frame = CGRectMake(i%4 * width, i / 4 * height, width, height);
  71. }];
  72. cell.playRecordBlock = ^(CWVoiceChangePlayCell *cellBlock) {
  73. [weakSelf.playTimer invalidate];
  74. if (weakSelf.playingView != cellBlock) {
  75. [weakSelf.playingView endPlay];
  76. }
  77. [cellBlock playingRecord];
  78. weakSelf.playingView = cellBlock;
  79. [weakSelf startPlayTimer];
  80. };
  81. cell.endPlayBlock = ^(CWVoiceChangePlayCell *cellBlock) {
  82. [weakSelf.playTimer invalidate];
  83. [cellBlock endPlay];
  84. };
  85. if (i == self.imageNames.count - 1) {
  86. CGFloat h = i / 4 * height;
  87. if (h < self.cw_height - self.cancelButton.cw_height) h = self.cw_height - self.cancelButton.cw_height + 1;
  88. self.contentScrollView.contentSize = CGSizeMake(0, h);
  89. }
  90. }
  91. }
  92. - (void)setupSendButtonAndCancelButton {
  93. CGFloat height = 40;
  94. 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:)];
  95. [self addSubview:cancelBtn];
  96. self.cancelButton = cancelBtn;
  97. 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:)];
  98. [self addSubview:sendBtn];
  99. self.sendButton = sendBtn;
  100. }
  101. - (UIButton *)buttonWithFrame:(CGRect)frame title:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font backImageNor:(NSString *)backImageNor backImageHighled:(NSString *)backImageHighled sel:(SEL)sel{
  102. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  103. btn.frame = frame;
  104. [btn setTitle:title forState:UIControlStateNormal];
  105. [btn setTitleColor:titleColor forState:UIControlStateNormal];
  106. btn.titleLabel.font = font;
  107. UIImage *newImageNor = [[UIImage imageNamed:backImageNor] stretchableImageWithLeftCapWidth:2 topCapHeight:2];
  108. UIImage *newImageHighled = [[UIImage imageNamed:backImageHighled] stretchableImageWithLeftCapWidth:2 topCapHeight:2];
  109. [btn setBackgroundImage:newImageNor forState:UIControlStateNormal];
  110. [btn setBackgroundImage:newImageHighled forState:UIControlStateHighlighted];
  111. [btn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
  112. return btn;
  113. }
  114. #pragma mark - playTimer
  115. - (void)startPlayTimer {
  116. // _allCount = self.allLevels.count;
  117. [self.playTimer invalidate];
  118. self.playTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(updatePlayMeter)];
  119. if ([[UIDevice currentDevice].systemVersion floatValue] > 10.0) {
  120. self.playTimer.preferredFramesPerSecond = 10;
  121. }else {
  122. self.playTimer.frameInterval = 6;
  123. }
  124. [self.playTimer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  125. }
  126. - (void)updatePlayMeter {
  127. [self.playingView updateLevels];
  128. }
  129. - (void)stopPlay {
  130. [[CWAudioPlayer shareInstance] stopCurrentAudio];
  131. }
  132. - (void)btnClick:(UIButton *)btn {
  133. // NSLog(@"%@",btn.titleLabel.text);
  134. [self stopPlay];
  135. if (btn == self.sendButton) { // 发送
  136. NSLog(ASLocalizedString(@"发送...path: %@"),self.playingView.voicePath);
  137. NSDictionary *info = @{
  138. @"path":self.playingView.voicePath };
  139. KPostNotification(KRecordingEnd, info);
  140. }else {
  141. NSLog(ASLocalizedString(@"取消发送并删除录音/删除变声文件"));
  142. [[CWRecorder shareInstance] deleteRecord];
  143. [CWFlieManager removeFile:[CWFlieManager soundTouchSavePathWithFileName:self.playingView.voicePath.lastPathComponent]];
  144. }
  145. [(CWVoiceView *)self.superview.superview.superview setState:CWVoiceStateDefault];
  146. [UIView transitionWithView:self duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
  147. [self removeFromSuperview];
  148. } completion:nil];
  149. }
  150. @end