diff --git a/Example/MessageDisplayKitCoreDataExample/MessageDisplayKitCoreDataExample/MessageDisplayKitCoreDataExample-Info.plist b/Example/MessageDisplayKitCoreDataExample/MessageDisplayKitCoreDataExample/MessageDisplayKitCoreDataExample-Info.plist
index 4b22ba4..22f4f10 100644
--- a/Example/MessageDisplayKitCoreDataExample/MessageDisplayKitCoreDataExample/MessageDisplayKitCoreDataExample-Info.plist
+++ b/Example/MessageDisplayKitCoreDataExample/MessageDisplayKitCoreDataExample/MessageDisplayKitCoreDataExample-Info.plist
@@ -44,5 +44,10 @@
UIInterfaceOrientationPortrait
+ NSAppTransportSecurity
+
+ NSAllowsArbitraryLoads
+
+
diff --git a/Example/MessageDisplayKitLeanchatExample/MessageDisplayKitLeanchatExample/Info.plist b/Example/MessageDisplayKitLeanchatExample/MessageDisplayKitLeanchatExample/Info.plist
index 02ba115..8b0a410 100644
--- a/Example/MessageDisplayKitLeanchatExample/MessageDisplayKitLeanchatExample/Info.plist
+++ b/Example/MessageDisplayKitLeanchatExample/MessageDisplayKitLeanchatExample/Info.plist
@@ -34,6 +34,8 @@
+ NSAllowsArbitraryLoads
+
UILaunchStoryboardName
LaunchScreen
diff --git a/Example/MessageDisplayKitStoryBoradExample/MessageDisplayKitStoryBoradExample/MessageDisplayKitStoryBoradExample-Info.plist b/Example/MessageDisplayKitStoryBoradExample/MessageDisplayKitStoryBoradExample/MessageDisplayKitStoryBoradExample-Info.plist
index 5308c10..636972c 100644
--- a/Example/MessageDisplayKitStoryBoradExample/MessageDisplayKitStoryBoradExample/MessageDisplayKitStoryBoradExample-Info.plist
+++ b/Example/MessageDisplayKitStoryBoradExample/MessageDisplayKitStoryBoradExample/MessageDisplayKitStoryBoradExample-Info.plist
@@ -49,5 +49,10 @@
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
+ NSAppTransportSecurity
+
+ NSAllowsArbitraryLoads
+
+
diff --git a/Example/MessageDisplayKitWeChatExample/MessageDisplayKitWeChatExample/Info.plist b/Example/MessageDisplayKitWeChatExample/MessageDisplayKitWeChatExample/Info.plist
index ddfb75f..005e2c0 100644
--- a/Example/MessageDisplayKitWeChatExample/MessageDisplayKitWeChatExample/Info.plist
+++ b/Example/MessageDisplayKitWeChatExample/MessageDisplayKitWeChatExample/Info.plist
@@ -37,5 +37,10 @@
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
+ NSAppTransportSecurity
+
+ NSAllowsArbitraryLoads
+
+
diff --git a/MessageDisplayKit/Classes/Common/XHVoiceRecordHelper.h b/MessageDisplayKit/Classes/Common/XHVoiceRecordHelper.h
index 3f64805..2180314 100644
--- a/MessageDisplayKit/Classes/Common/XHVoiceRecordHelper.h
+++ b/MessageDisplayKit/Classes/Common/XHVoiceRecordHelper.h
@@ -16,6 +16,7 @@ typedef void(^XHResumeRecorderCompletion)();
typedef void(^XHCancellRecorderDeleteFileCompletion)();
typedef void(^XHRecordProgress)(float progress);
typedef void(^XHPeakPowerForChannel)(float peakPowerForChannel);
+typedef void(^XHRecordingRemainTime)(NSInteger remainTime);
@interface XHVoiceRecordHelper : NSObject
@@ -23,6 +24,7 @@ typedef void(^XHPeakPowerForChannel)(float peakPowerForChannel);
@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秒为最大
diff --git a/MessageDisplayKit/Classes/Common/XHVoiceRecordHelper.m b/MessageDisplayKit/Classes/Common/XHVoiceRecordHelper.m
index 26bf57f..4b34f6e 100644
--- a/MessageDisplayKit/Classes/Common/XHVoiceRecordHelper.m
+++ b/MessageDisplayKit/Classes/Common/XHVoiceRecordHelper.m
@@ -12,7 +12,7 @@
@interface XHVoiceRecordHelper () {
NSTimer *_timer;
-
+ NSTimer *_recordTimer;
BOOL _isPause;
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
@@ -22,7 +22,7 @@ @interface XHVoiceRecordHelper () {
@property (nonatomic, copy, readwrite) NSString *recordPath;
@property (nonatomic, readwrite) NSTimeInterval currentTimeInterval;
-
+@property (nonatomic, readwrite) NSInteger remainRecordTime;
@property (nonatomic, strong) AVAudioRecorder *recorder;
@end
@@ -67,7 +67,7 @@ - (void)stopBackgroundTask {
}
- (void)resetTimer {
- if (!_timer)
+ if (!_timer && !_recordTimer)
return;
if (_timer) {
@@ -75,6 +75,10 @@ - (void)resetTimer {
_timer = nil;
}
+ if (_recordTimer) {
+ [_recordTimer invalidate];
+ _recordTimer = nil;
+ }
}
- (void)cancelRecording {
@@ -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();
@@ -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];
@@ -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];
@@ -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)];
}
}
diff --git a/MessageDisplayKit/Classes/Controllers/XHMessageTableViewController/XHMessageTableViewController.m b/MessageDisplayKit/Classes/Controllers/XHMessageTableViewController/XHMessageTableViewController.m
index cde70af..69cbe3f 100644
--- a/MessageDisplayKit/Classes/Controllers/XHMessageTableViewController/XHMessageTableViewController.m
+++ b/MessageDisplayKit/Classes/Controllers/XHMessageTableViewController/XHMessageTableViewController.m
@@ -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;
diff --git a/MessageDisplayKit/Classes/Views/MessageInputView/XHVoiceRecordHUD.h b/MessageDisplayKit/Classes/Views/MessageInputView/XHVoiceRecordHUD.h
index 141ebd4..697d4aa 100644
--- a/MessageDisplayKit/Classes/Views/MessageInputView/XHVoiceRecordHUD.h
+++ b/MessageDisplayKit/Classes/Views/MessageInputView/XHVoiceRecordHUD.h
@@ -10,7 +10,8 @@
@interface XHVoiceRecordHUD : UIView
-@property (nonatomic, assign) CGFloat peakPower;
+@property (nonatomic, assign) NSInteger remainTime; // 录音剩余时间
+@property (nonatomic, assign) CGFloat peakPower; // 录音时声音分贝大小
/**
* 开始显示录音HUD控件在某个view
diff --git a/MessageDisplayKit/Classes/Views/MessageInputView/XHVoiceRecordHUD.m b/MessageDisplayKit/Classes/Views/MessageInputView/XHVoiceRecordHUD.m
index 46e93c1..96d9869 100644
--- a/MessageDisplayKit/Classes/Views/MessageInputView/XHVoiceRecordHUD.m
+++ b/MessageDisplayKit/Classes/Views/MessageInputView/XHVoiceRecordHUD.m
@@ -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;
@@ -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 {
@@ -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;
@@ -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 {