-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathUIMessageInputView_Voice.m
188 lines (160 loc) · 6.49 KB
/
UIMessageInputView_Voice.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// UIMessageInputView_Voice.m
// Coding_iOS
//
// Created by sumeng on 8/1/15.
// Copyright (c) 2015 Coding. All rights reserved.
//
#import "UIMessageInputView_Voice.h"
#import "AudioRecordView.h"
#import "AudioVolumeView.h"
#import "AudioManager.h"
typedef NS_ENUM(NSInteger, UIMessageInputView_VoiceState) {
UIMessageInputView_VoiceStateReady,
UIMessageInputView_VoiceStateRecording,
UIMessageInputView_VoiceStateCancel
};
@interface UIMessageInputView_Voice () <AudioRecordViewDelegate>
@property (strong, nonatomic) UILabel *recordTipsLabel;
@property (strong, nonatomic) AudioRecordView *recordView;
@property (strong, nonatomic) AudioVolumeView *volumeLeftView;
@property (strong, nonatomic) AudioVolumeView *volumeRightView;
@property (assign, nonatomic) UIMessageInputView_VoiceState state;
@property (assign, nonatomic) int duration;
@property (strong, nonatomic) NSTimer *timer;
@end
@implementation UIMessageInputView_Voice
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = kColorNavBG;
_recordTipsLabel = [[UILabel alloc] init];
_recordTipsLabel.font = [UIFont systemFontOfSize:18];
[self addSubview:_recordTipsLabel];
_volumeLeftView = [[AudioVolumeView alloc] initWithFrame:CGRectMake(0, 0, kAudioVolumeViewWidth, kAudioVolumeViewHeight)];
_volumeLeftView.type = AudioVolumeViewTypeLeft;
_volumeLeftView.hidden = YES;
[self addSubview:_volumeLeftView];
_volumeRightView = [[AudioVolumeView alloc] initWithFrame:CGRectMake(0, 0, kAudioVolumeViewWidth, kAudioVolumeViewHeight)];
_volumeRightView.type = AudioVolumeViewTypeRight;
_volumeRightView.hidden = YES;
[self addSubview:_volumeRightView];
_recordView = [[AudioRecordView alloc] initWithFrame:CGRectMake((self.frame.size.width - 86) / 2, 62, 86, 86)];
_recordView.delegate = self;
[self addSubview:_recordView];
UILabel *tipLabel = [[UILabel alloc] init];
tipLabel.font = [UIFont systemFontOfSize:12];
tipLabel.textColor = [UIColor colorWithRGBHex:0x999999];
tipLabel.text = @"向上滑动,取消发送";
[tipLabel sizeToFit];
tipLabel.center = CGPointMake(self.frame.size.width/2, self.frame.size.height-25);
[self addSubview:tipLabel];
_duration = 0;
self.state = UIMessageInputView_VoiceStateReady;
}
return self;
}
- (void)dealloc {
self.state = UIMessageInputView_VoiceStateReady;
}
- (void)setState:(UIMessageInputView_VoiceState)state {
_state = state;
switch (state) {
case UIMessageInputView_VoiceStateReady:
_recordTipsLabel.textColor = [UIColor colorWithRGBHex:0x999999];
_recordTipsLabel.text = @"按住说话";
_volumeLeftView.hidden = YES;
_volumeRightView.hidden = YES;
break;
case UIMessageInputView_VoiceStateRecording:
if (_duration < ([AudioManager shared].maxRecordDuration - 5)) {
_recordTipsLabel.textColor = [UIColor colorWithRGBHex:0x2faeea];
}
else {
_recordTipsLabel.textColor = [UIColor colorWithRGBHex:0xDE4743];
}
_recordTipsLabel.text = [self formattedTime:_duration];
break;
case UIMessageInputView_VoiceStateCancel:
_recordTipsLabel.textColor = [UIColor colorWithRGBHex:0x999999];
_recordTipsLabel.text = @"松开取消";
_volumeLeftView.hidden = YES;
_volumeRightView.hidden = YES;
break;
default:
break;
}
[_recordTipsLabel sizeToFit];
_recordTipsLabel.center = CGPointMake(self.frame.size.width/2, 20);
if (state == UIMessageInputView_VoiceStateRecording) {
_volumeLeftView.center = CGPointMake(_recordTipsLabel.frame.origin.x - _volumeLeftView.frame.size.width/2 - 12, _recordTipsLabel.center.y);
_volumeLeftView.hidden = NO;
_volumeRightView.center = CGPointMake(_recordTipsLabel.frame.origin.x + _recordTipsLabel.frame.size.width + _volumeRightView.frame.size.width/2 + 12, _recordTipsLabel.center.y);
_volumeRightView.hidden = NO;
}
}
#pragma mark - RecordTimer
- (void)startTimer {
_duration = 0;
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(increaseRecordTime) userInfo:nil repeats:YES];
}
- (void)stopTimer {
if (_timer) {
[_timer invalidate];
self.timer = nil;
}
}
- (void)increaseRecordTime {
_duration++;
if (self.state == UIMessageInputView_VoiceStateRecording) {
//update time label
self.state = UIMessageInputView_VoiceStateRecording;
}
}
- (NSString *)formattedTime:(int)duration {
return [NSString stringWithFormat:@"%02d:%02d", duration / 60, duration % 60];
}
#pragma mark - AudioRecordViewDelegate
- (void)recordViewRecordStarted:(AudioRecordView *)recordView {
[_volumeLeftView clearVolume];
[_volumeRightView clearVolume];
self.state = UIMessageInputView_VoiceStateRecording;
[self startTimer];
}
- (void)recordViewRecordFinished:(AudioRecordView *)recordView file:(NSString *)file duration:(NSTimeInterval)duration {
[self stopTimer];
if (self.state == UIMessageInputView_VoiceStateRecording) {
if (_recordSuccessfully) {
_recordSuccessfully(file, duration);
}
}
else if (self.state == UIMessageInputView_VoiceStateCancel) {
//remove record file
[[NSFileManager defaultManager] removeItemAtPath:file error:nil];
}
self.state = UIMessageInputView_VoiceStateReady;
_duration = 0;
}
- (void)recordView:(AudioRecordView *)recordView touchStateChanged:(AudioRecordViewTouchState)touchState {
if (self.state != UIMessageInputView_VoiceStateReady) {
if (touchState == AudioRecordViewTouchStateInside) {
self.state = UIMessageInputView_VoiceStateRecording;
}
else {
self.state = UIMessageInputView_VoiceStateCancel;
}
}
}
- (void)recordView:(AudioRecordView *)recordView volume:(double)volume {
[_volumeLeftView addVolume:volume];
[_volumeRightView addVolume:volume];
}
- (void)recordViewRecord:(AudioRecordView *)recordView err:(NSError *)err {
[self stopTimer];
if (self.state == UIMessageInputView_VoiceStateRecording) {
[NSObject showHudTipStr:err.domain];
}
self.state = UIMessageInputView_VoiceStateReady;
_duration = 0;
}
@end