Skip to content

Commit

Permalink
录音动画显示当前录音时间
Browse files Browse the repository at this point in the history
  • Loading branch information
gyjzh committed Nov 19, 2016
1 parent a6577a6 commit 4dbf949
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions LLWeChat/Client/Chat/Cell/LLMessageRecordingCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@

+ (instancetype)sharedRecordingCell;

- (void)updateDurationLabel:(int)duration;

@end
17 changes: 17 additions & 0 deletions LLWeChat/Client/Chat/Cell/LLMessageRecordingCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "LLMessageRecordingCell.h"
#import "UIKit+LLExt.h"
#import "LLMessageVoiceCell.h"
#import "LLColors.h"

#define RECORD_ANIMATION_KEY @"RecordAnimate"

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

@property (nonatomic) UIImageView *downloadingImageView;

@property (nonatomic) UILabel *durationLabel;

@property (nonatomic) CAKeyframeAnimation *keyFrameAnimation;

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

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

_durationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, OFFSET_Y, 60, AVATAR_HEIGHT)];
_durationLabel.textColor = kLLTextColor_lightGray_7;
_durationLabel.font = [UIFont systemFontOfSize:16];
_durationLabel.textAlignment = NSTextAlignmentRight;
[self.contentView addSubview:_durationLabel];

}

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

self.downloadingImageView.frame = self.bubbleImage.frame;
[self.bubbleImage.layer addAnimation:self.keyFrameAnimation forKey:RECORD_ANIMATION_KEY];

frame = self.durationLabel.frame;
frame.origin.x = CGRectGetMinX(self.bubbleImage.frame) - CGRectGetWidth(frame) - 8 + BUBBLE_LEFT_BLANK;
self.durationLabel.frame = frame;
}
}

Expand Down Expand Up @@ -109,6 +122,10 @@ - (void)setMessageModel:(LLMessageModel *)messageModel {
}


- (void)updateDurationLabel:(int)duration {
_durationLabel.text = [NSString stringWithFormat:@"%d'", duration];
}

+ (CGFloat)heightForModel:(LLMessageModel *)model {
return AVATAR_HEIGHT + CONTENT_SUPER_BOTTOM + OFFSET_Y;
}
Expand Down
4 changes: 4 additions & 0 deletions LLWeChat/Client/Chat/LLChatViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,10 @@ - (void)audioRecordDidUpdateVoiceMeter:(double)averagePower {
}
}

- (void)audioRecordDurationDidChanged:(NSTimeInterval)duration {
[[LLMessageRecordingCell sharedRecordingCell] updateDurationLabel:round(duration)];
}

- (LLMessageModel *)getRecordingModel {
for (NSInteger i = self.dataSource.count - 1; i >= 0; i--) {
if (self.dataSource[i].messageBodyType == kLLMessageBodyTypeRecording) {
Expand Down
12 changes: 11 additions & 1 deletion LLWeChat/General/Audio/LLAudioManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ @interface LLAudioManager () <AVAudioRecorderDelegate, AVAudioPlayerDelegate>
@implementation LLAudioManager {
CFTimeInterval startTime;
NSTimeInterval maxRecordTime;
NSTimer *timer;

NSDate *startDate;
// NSDate *endDate;
Expand Down Expand Up @@ -234,6 +235,13 @@ - (void)startRecordingWithDelegate:(id<LLAudioRecordDelegate>)delegate
self.isCancelRecording = NO;
self.isRecording = YES;
self.recorder.delegate = self;

[timer invalidate];
SAFE_SEND_MESSAGE(self.recordDelegate, audioRecordDurationDidChanged:) {
timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerHandler:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

//开启仪表计数功能,可以获取当前录音音量大小
self.recorder.meteringEnabled = YES;
maxRecordTime = MAX_RECORD_TIME_ALLOWED;
Expand Down Expand Up @@ -323,7 +331,9 @@ - (void)updateVoiceMeter {
});
}


- (void)timerHandler:(NSTimer *)timer {
[self.recordDelegate audioRecordDurationDidChanged:self.recorder.currentTime];
}

/**
* 停止录音
Expand Down
3 changes: 3 additions & 0 deletions LLWeChat/General/Audio/LLAudioRecordDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
*/
- (void)audioRecordDidUpdateVoiceMeter:(double)averagePower;

//录音时长变化,以秒为单位
- (void)audioRecordDurationDidChanged:(NSTimeInterval)duration;

//录音最长时间,默认为MAX_RECORD_TIME_ALLOWED = 60秒
- (NSTimeInterval)audioRecordMaxRecordTime;

Expand Down

0 comments on commit 4dbf949

Please sign in to comment.