Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加录制语音10秒倒计时的小功能 #325

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,10 @@
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
<true/>
</dict>
</dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,10 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,10 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
</dict>
</plist>
2 changes: 2 additions & 0 deletions MessageDisplayKit/Classes/Common/XHVoiceRecordHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ typedef void(^XHResumeRecorderCompletion)();
typedef void(^XHCancellRecorderDeleteFileCompletion)();
typedef void(^XHRecordProgress)(float progress);
typedef void(^XHPeakPowerForChannel)(float peakPowerForChannel);
typedef void(^XHRecordingRemainTime)(NSInteger remainTime);


@interface XHVoiceRecordHelper : NSObject

@property (nonatomic, copy) XHStopRecorderCompletion maxTimeStopRecorderCompletion;
@property (nonatomic, copy) XHRecordProgress recordProgress;
@property (nonatomic, copy) XHPeakPowerForChannel peakPowerForChannel;
@property (nonatomic, copy) XHRecordingRemainTime remainTime;
@property (nonatomic, copy, readonly) NSString *recordPath;
@property (nonatomic, copy) NSString *recordDuration;
@property (nonatomic) float maxRecordTime; // 默认 60秒为最大
Expand Down
43 changes: 33 additions & 10 deletions MessageDisplayKit/Classes/Common/XHVoiceRecordHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@interface XHVoiceRecordHelper () <AVAudioRecorderDelegate> {
NSTimer *_timer;

NSTimer *_recordTimer;
BOOL _isPause;

#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
Expand All @@ -22,7 +22,7 @@ @interface XHVoiceRecordHelper () <AVAudioRecorderDelegate> {

@property (nonatomic, copy, readwrite) NSString *recordPath;
@property (nonatomic, readwrite) NSTimeInterval currentTimeInterval;

@property (nonatomic, readwrite) NSInteger remainRecordTime;
@property (nonatomic, strong) AVAudioRecorder *recorder;

@end
Expand Down Expand Up @@ -67,14 +67,18 @@ - (void)stopBackgroundTask {
}

- (void)resetTimer {
if (!_timer)
if (!_timer && !_recordTimer)
return;

if (_timer) {
[_timer invalidate];
_timer = nil;
}

if (_recordTimer) {
[_recordTimer invalidate];
_recordTimer = nil;
}
}

- (void)cancelRecording {
Expand Down Expand Up @@ -155,6 +159,8 @@ - (void)startRecordingWithStartRecorderCompletion:(XHStartRecorderCompletion)sta
if ([_recorder record]) {
[self resetTimer];
_timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(updateMeters) userInfo:nil repeats:YES];
_remainRecordTime = kVoiceRecorderTotalTime;
_recordTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCurrentLeftTime) userInfo:nil repeats:YES];
if (startRecorderCompletion)
dispatch_async(dispatch_get_main_queue(), ^{
startRecorderCompletion();
Expand Down Expand Up @@ -240,12 +246,19 @@ - (void)updateMeters {
double ALPHA = 0.015;
double peakPowerForChannel = pow(10, (ALPHA * peakPower));

dispatch_async(dispatch_get_main_queue(), ^{
// 更新扬声器
if (_peakPowerForChannel) {
_peakPowerForChannel(peakPowerForChannel);
}
});
// 返回录音剩余时间
if (self.remainRecordTime <= 10) {
dispatch_async(dispatch_get_main_queue(), ^{
_remainTime(self.remainRecordTime);
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
// 更新扬声器
if (_peakPowerForChannel) {
_peakPowerForChannel(peakPowerForChannel);
}
});
}

if (self.currentTimeInterval > self.maxRecordTime) {
[self stopRecord];
Expand All @@ -256,6 +269,16 @@ - (void)updateMeters {
});
}

/// 实时更新当前录音的剩余时间
- (void)updateCurrentLeftTime {
if (self.remainRecordTime >= 0) {
self.remainRecordTime --;
} else {
[_recordTimer invalidate];
_recordTimer = nil;
}
}

- (void)getVoiceDuration:(NSString*)recordPath {
NSError *error = nil;
AVAudioPlayer *play = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:recordPath] error:&error];
Expand All @@ -264,7 +287,7 @@ - (void)getVoiceDuration:(NSString*)recordPath {
self.recordDuration = @"";
} else {
DLog(@"时长:%f", play.duration);
self.recordDuration = [NSString stringWithFormat:@"%.1f", play.duration];
self.recordDuration = [NSString stringWithFormat:@"%d", (int)roundf(play.duration)];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ - (XHVoiceRecordHelper *)voiceRecordHelper {
_voiceRecordHelper.peakPowerForChannel = ^(float peakPowerForChannel) {
weakSelf.voiceRecordHUD.peakPower = peakPowerForChannel;
};
_voiceRecordHelper.remainTime = ^(NSInteger remainTime) {
weakSelf.voiceRecordHUD.remainTime = remainTime;
};
_voiceRecordHelper.maxRecordTime = kVoiceRecorderTotalTime;
}
return _voiceRecordHelper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

@interface XHVoiceRecordHUD : UIView

@property (nonatomic, assign) CGFloat peakPower;
@property (nonatomic, assign) NSInteger remainTime; // 录音剩余时间
@property (nonatomic, assign) CGFloat peakPower; // 录音时声音分贝大小

/**
* 开始显示录音HUD控件在某个view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@interface XHVoiceRecordHUD ()

@property (nonatomic, weak) UILabel *remindLabel;
@property (nonatomic, weak) UILabel *remainTimeLabel;
@property (nonatomic, weak) UIImageView *microPhoneImageView;
@property (nonatomic, weak) UIImageView *cancelRecordImageView;
@property (nonatomic, weak) UIImageView *recordingHUDImageView;
Expand Down Expand Up @@ -82,9 +83,14 @@ - (void)dismissCompled:(void(^)(BOOL fnished))compled {
}

- (void)configRecoding:(BOOL)recording {
self.microPhoneImageView.hidden = !recording;
self.recordingHUDImageView.hidden = !recording;
self.cancelRecordImageView.hidden = recording;
if (self.remainTime > 0) {
[self configViewsWithRemainTime:self.remainTime];
} else {
self.microPhoneImageView.hidden = !recording;
self.recordingHUDImageView.hidden = !recording;
self.cancelRecordImageView.hidden = recording;
self.remainTimeLabel.hidden = recording;
}
}

- (void)configRecordingHUDImageWithPeakPower:(CGFloat)peakPower {
Expand All @@ -109,11 +115,24 @@ - (void)configRecordingHUDImageWithPeakPower:(CGFloat)peakPower {
self.recordingHUDImageView.image = [UIImage imageNamed:imageName];
}

- (void)configViewsWithRemainTime:(NSInteger)remainTime {
self.microPhoneImageView.hidden = YES;
self.recordingHUDImageView.hidden = YES;
self.cancelRecordImageView.hidden = YES;
self.remainTimeLabel.hidden = NO;
self.remainTimeLabel.text = @(remainTime).stringValue;
}

- (void)setPeakPower:(CGFloat)peakPower {
_peakPower = peakPower;
[self configRecordingHUDImageWithPeakPower:peakPower];
}

- (void)setRemainTime:(NSInteger)remainTime {
_remainTime = remainTime;
[self configViewsWithRemainTime:remainTime];
}

- (void)setup {
self.backgroundColor = [UIColor blackColor];
self.layer.masksToBounds = YES;
Expand Down Expand Up @@ -160,6 +179,16 @@ - (void)setup {
[self addSubview:cancelRecordImageView];
_cancelRecordImageView = cancelRecordImageView;
}

if (!_remainTimeLabel) {
UILabel *remainTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 120, 110)];
remainTimeLabel.backgroundColor = [UIColor clearColor];
remainTimeLabel.textColor = [UIColor whiteColor];
remainTimeLabel.textAlignment = NSTextAlignmentCenter;
remainTimeLabel.font = [UIFont systemFontOfSize:60];
[self addSubview:remainTimeLabel];
_remainTimeLabel = remainTimeLabel;
}
}

- (id)initWithFrame:(CGRect)frame {
Expand Down