Skip to content

Commit 4dbf949

Browse files
committed
录音动画显示当前录音时间
1 parent a6577a6 commit 4dbf949

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

LLWeChat/Client/Chat/Cell/LLMessageRecordingCell.h

+2
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212

1313
+ (instancetype)sharedRecordingCell;
1414

15+
- (void)updateDurationLabel:(int)duration;
16+
1517
@end

LLWeChat/Client/Chat/Cell/LLMessageRecordingCell.m

+17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "LLMessageRecordingCell.h"
1010
#import "UIKit+LLExt.h"
1111
#import "LLMessageVoiceCell.h"
12+
#import "LLColors.h"
1213

1314
#define RECORD_ANIMATION_KEY @"RecordAnimate"
1415

@@ -18,6 +19,8 @@ @interface LLMessageRecordingCell ()
1819

1920
@property (nonatomic) UIImageView *downloadingImageView;
2021

22+
@property (nonatomic) UILabel *durationLabel;
23+
2124
@property (nonatomic) CAKeyframeAnimation *keyFrameAnimation;
2225

2326
@end
@@ -44,6 +47,12 @@ - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSStr
4447

4548
[self.contentView insertSubview:self.downloadingImageView belowSubview:self.bubbleImage];
4649

50+
_durationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, OFFSET_Y, 60, AVATAR_HEIGHT)];
51+
_durationLabel.textColor = kLLTextColor_lightGray_7;
52+
_durationLabel.font = [UIFont systemFontOfSize:16];
53+
_durationLabel.textAlignment = NSTextAlignmentRight;
54+
[self.contentView addSubview:_durationLabel];
55+
4756
}
4857

4958
return self;
@@ -67,6 +76,10 @@ - (void)prepareForUse:(BOOL)isFromMe {
6776

6877
self.downloadingImageView.frame = self.bubbleImage.frame;
6978
[self.bubbleImage.layer addAnimation:self.keyFrameAnimation forKey:RECORD_ANIMATION_KEY];
79+
80+
frame = self.durationLabel.frame;
81+
frame.origin.x = CGRectGetMinX(self.bubbleImage.frame) - CGRectGetWidth(frame) - 8 + BUBBLE_LEFT_BLANK;
82+
self.durationLabel.frame = frame;
7083
}
7184
}
7285

@@ -109,6 +122,10 @@ - (void)setMessageModel:(LLMessageModel *)messageModel {
109122
}
110123

111124

125+
- (void)updateDurationLabel:(int)duration {
126+
_durationLabel.text = [NSString stringWithFormat:@"%d'", duration];
127+
}
128+
112129
+ (CGFloat)heightForModel:(LLMessageModel *)model {
113130
return AVATAR_HEIGHT + CONTENT_SUPER_BOTTOM + OFFSET_Y;
114131
}

LLWeChat/Client/Chat/LLChatViewController.m

+4
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,10 @@ - (void)audioRecordDidUpdateVoiceMeter:(double)averagePower {
18611861
}
18621862
}
18631863

1864+
- (void)audioRecordDurationDidChanged:(NSTimeInterval)duration {
1865+
[[LLMessageRecordingCell sharedRecordingCell] updateDurationLabel:round(duration)];
1866+
}
1867+
18641868
- (LLMessageModel *)getRecordingModel {
18651869
for (NSInteger i = self.dataSource.count - 1; i >= 0; i--) {
18661870
if (self.dataSource[i].messageBodyType == kLLMessageBodyTypeRecording) {

LLWeChat/General/Audio/LLAudioManager.m

+11-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ @interface LLAudioManager () <AVAudioRecorderDelegate, AVAudioPlayerDelegate>
4848
@implementation LLAudioManager {
4949
CFTimeInterval startTime;
5050
NSTimeInterval maxRecordTime;
51+
NSTimer *timer;
5152

5253
NSDate *startDate;
5354
// NSDate *endDate;
@@ -234,6 +235,13 @@ - (void)startRecordingWithDelegate:(id<LLAudioRecordDelegate>)delegate
234235
self.isCancelRecording = NO;
235236
self.isRecording = YES;
236237
self.recorder.delegate = self;
238+
239+
[timer invalidate];
240+
SAFE_SEND_MESSAGE(self.recordDelegate, audioRecordDurationDidChanged:) {
241+
timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerHandler:) userInfo:nil repeats:YES];
242+
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
243+
}
244+
237245
//开启仪表计数功能,可以获取当前录音音量大小
238246
self.recorder.meteringEnabled = YES;
239247
maxRecordTime = MAX_RECORD_TIME_ALLOWED;
@@ -323,7 +331,9 @@ - (void)updateVoiceMeter {
323331
});
324332
}
325333

326-
334+
- (void)timerHandler:(NSTimer *)timer {
335+
[self.recordDelegate audioRecordDurationDidChanged:self.recorder.currentTime];
336+
}
327337

328338
/**
329339
* 停止录音

LLWeChat/General/Audio/LLAudioRecordDelegate.h

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
*/
2626
- (void)audioRecordDidUpdateVoiceMeter:(double)averagePower;
2727

28+
//录音时长变化,以秒为单位
29+
- (void)audioRecordDurationDidChanged:(NSTimeInterval)duration;
30+
2831
//录音最长时间,默认为MAX_RECORD_TIME_ALLOWED = 60秒
2932
- (NSTimeInterval)audioRecordMaxRecordTime;
3033

0 commit comments

Comments
 (0)