| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- //
- // CWAudioPlayView.m
- // QQVoiceDemo
- //
- // Created by 陈旺 on 2017/10/4.
- // Copyright © 2017年 陈旺. All rights reserved.
- //
- #import "CWAudioPlayView.h"
- #import "UIView+CWChat.h"
- #import "CWRecordStateView.h"
- #import "CWAudioPlayer.h"
- #import "CWRecordModel.h"
- #import "CWRecorder.h"
- #import "CWVoiceView.h"
- @interface CWAudioPlayView ()
- @property (nonatomic, weak) CWRecordStateView *stateView;
- @property (nonatomic, weak) UIButton *playButton; // 播放按钮
- @property (nonatomic, weak) UIButton *cancelButton; // 取消按钮
- @property (nonatomic, weak) UIButton *sendButton; // 发送按钮
- @end
- @implementation CWAudioPlayView
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // _progressValue = 0.8;
- [self setupSubViews];
- }
- return self;
- }
- - (void)setupSubViews {
-
- self.backgroundColor = [UIColor whiteColor];
- [self stateView];
- [self playButton];
- [self setupSendButtonAndCancelButton];
- [self listenProgress]; // 监听进度
- }
- #pragma mark - subviews
- - (UIButton *)playButton {
- if (_playButton == nil) {
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
- [btn setImage:[UIImage imageNamed:@"aio_record_play_nor"] forState:UIControlStateNormal];
- [btn setImage:[UIImage imageNamed:@"aio_record_play_press"] forState:UIControlStateHighlighted];
- [btn setImage:[UIImage imageNamed:@"aio_record_stop_nor"] forState:UIControlStateSelected];
- UIImage *image = [UIImage imageNamed:@"aio_voice_button_nor"];
- btn.frame = CGRectMake(0, 0, image.size.width, image.size.height);
- btn.center = CGPointMake(self.center.x, self.stateView.cw_bottom + image.size.width / 2);
- [btn addTarget:self action:@selector(playRecord) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:btn];
- _playButton = btn;
- }
- return _playButton;
- }
- - (CWRecordStateView *)stateView {
- if (_stateView == nil) {
- CWRecordStateView *stateView = [[CWRecordStateView alloc] initWithFrame:CGRectMake(0, 10, self.cw_width, 50)];
- [self addSubview:stateView];
- stateView.recordState = CWRecordStatePreparePlay;
- _stateView = stateView;
- }
- return _stateView;
- }
- - (void)setupSendButtonAndCancelButton {
- CGFloat height = 40;
- 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:)];
- [self addSubview:cancelBtn];
- self.cancelButton = cancelBtn;
-
- 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:)];
- [self addSubview:sendBtn];
- self.sendButton = sendBtn;
-
- }
- - (UIButton *)buttonWithFrame:(CGRect)frame title:(NSString *)title titleColor:(UIColor *)titleColor font:(UIFont *)font backImageNor:(NSString *)backImageNor backImageHighled:(NSString *)backImageHighled sel:(SEL)sel{
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
- btn.frame = frame;
- [btn setTitle:title forState:UIControlStateNormal];
- [btn setTitleColor:titleColor forState:UIControlStateNormal];
- btn.titleLabel.font = font;
- UIImage *newImageNor = [[UIImage imageNamed:backImageNor] stretchableImageWithLeftCapWidth:2 topCapHeight:2];
- UIImage *newImageHighled = [[UIImage imageNamed:backImageHighled] stretchableImageWithLeftCapWidth:2 topCapHeight:2];
- [btn setBackgroundImage:newImageNor forState:UIControlStateNormal];
- [btn setBackgroundImage:newImageHighled forState:UIControlStateHighlighted];
- [btn addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
- return btn;
- }
- #pragma mark - play/stop
- - (void)playRecord {
- self.playButton.selected = !self.playButton.selected;
- if (self.playButton.selected) {
- self.stateView.recordState = CWRecordStatePlay;
- [[CWAudioPlayer shareInstance] playAudioWith:[CWRecordModel shareInstance].path];
- }else {
- [self stopPlay];
- }
- }
- - (void)stopPlay {
- self.playButton.selected = NO;
- self.stateView.recordState = CWRecordStatePreparePlay;
- [[CWAudioPlayer shareInstance] stopCurrentAudio];
- _progressValue = 0;
- [self setNeedsDisplay];
- [self layoutIfNeeded];
- }
- - (void)btnClick:(UIButton *)btn {
- // NSLog(@"%@",btn.titleLabel.text);
-
- [self stopPlay];
- if (btn == self.sendButton) { // 发送
- NSLog(ASLocalizedString(@"发送...path: %@"),[CWRecordModel shareInstance].path);
- KPostNotification(KRecordingEnd, @{});
- }else {
- NSLog(ASLocalizedString(@"取消发送并删除录音"));
- [[CWRecorder shareInstance] deleteRecord];
- }
- [(CWVoiceView *)self.superview.superview.superview setState:CWVoiceStateDefault];
- [self removeFromSuperview];
- }
- #pragma mark 监听环形进度条更新
- - (void)listenProgress {
- __weak typeof(self) weakSelf = self;
- self.stateView.playProgress = ^(CGFloat progress) {
- if (progress == 1) {
- progress = 0;
- [weakSelf stopPlay];
- }
- _progressValue = progress;
- [weakSelf setNeedsDisplay];
- [weakSelf layoutIfNeeded];
- };
- }
- - (void)drawRect:(CGRect)rect {
- [super drawRect:rect];
-
- UIImage *image = [UIImage imageNamed:@"aio_voice_button_nor"];
-
- CGContextRef ctx = UIGraphicsGetCurrentContext();
-
- CGContextSetLineWidth(ctx, 2.0f);
-
- CGContextSetStrokeColorWithColor(ctx, [UIColorFromRGBA(214, 219, 222, 1.0) CGColor]);
- CGContextAddArc(ctx, self.center.x, self.stateView.cw_bottom + image.size.width / 2, image.size.width / 2, 0, M_PI * 2, 0);
- CGContextStrokePath(ctx);
-
- CGContextSetStrokeColorWithColor(ctx, [kSelectBackGroudColor CGColor]);
- CGFloat startAngle = -M_PI_2;
- CGFloat angle = self.progressValue * M_PI * 2;
- CGFloat endAngle = startAngle + angle;
- CGContextAddArc(ctx, self.center.x, self.stateView.cw_bottom + image.size.width / 2, image.size.width / 2, startAngle, endAngle, 0);
- CGContextStrokePath(ctx);
-
- }
- @end
|