Skip to content

Commit d5071aa

Browse files
sumengsumeng
authored andcommitted
录音时获取音量信息
1 parent ad0ffd3 commit d5071aa

5 files changed

Lines changed: 180 additions & 2 deletions

File tree

Coding_iOS.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@
475475
7EB02FF11B6D111300D2166C /* AudioPlayView.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EB02FEC1B6D111300D2166C /* AudioPlayView.m */; };
476476
7EB02FF21B6D111300D2166C /* BubblePlayView.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EB02FEE1B6D111300D2166C /* BubblePlayView.m */; };
477477
7EB02FF31B6D111300D2166C /* AudioRecordView.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EB02FF01B6D111300D2166C /* AudioRecordView.m */; };
478+
7EB02FF61B6DAF3800D2166C /* AudioVolumeView.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EB02FF51B6DAF3800D2166C /* AudioVolumeView.m */; };
478479
8E03D4A219F695FE0092F8C8 /* libMobClickLibrary.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8E03D49F19F695FE0092F8C8 /* libMobClickLibrary.a */; };
479480
8E03D4A419F696970092F8C8 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 8E03D4A319F696970092F8C8 /* libz.dylib */; };
480481
8E1C3DEA19E7D58A00EF3032 /* AbstractActionSheetPicker.m in Sources */ = {isa = PBXBuildFile; fileRef = 8E1C3DDA19E7D58A00EF3032 /* AbstractActionSheetPicker.m */; };
@@ -1516,6 +1517,8 @@
15161517
7EB02FEE1B6D111300D2166C /* BubblePlayView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BubblePlayView.m; sourceTree = "<group>"; };
15171518
7EB02FEF1B6D111300D2166C /* AudioRecordView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AudioRecordView.h; sourceTree = "<group>"; };
15181519
7EB02FF01B6D111300D2166C /* AudioRecordView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AudioRecordView.m; sourceTree = "<group>"; };
1520+
7EB02FF41B6DAF3800D2166C /* AudioVolumeView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AudioVolumeView.h; sourceTree = "<group>"; };
1521+
7EB02FF51B6DAF3800D2166C /* AudioVolumeView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AudioVolumeView.m; sourceTree = "<group>"; };
15191522
8B18B0211092D3903D031B13 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = "<group>"; };
15201523
8E03D49F19F695FE0092F8C8 /* libMobClickLibrary.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libMobClickLibrary.a; sourceTree = "<group>"; };
15211524
8E03D4A019F695FE0092F8C8 /* MobClick.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MobClick.h; sourceTree = "<group>"; };
@@ -2926,6 +2929,8 @@
29262929
7EB02FEE1B6D111300D2166C /* BubblePlayView.m */,
29272930
7EB02FEF1B6D111300D2166C /* AudioRecordView.h */,
29282931
7EB02FF01B6D111300D2166C /* AudioRecordView.m */,
2932+
7EB02FF41B6DAF3800D2166C /* AudioVolumeView.h */,
2933+
7EB02FF51B6DAF3800D2166C /* AudioVolumeView.m */,
29292934
);
29302935
path = AudioView;
29312936
sourceTree = "<group>";
@@ -4565,6 +4570,7 @@
45654570
4ED673221A8E0ECC00DF2D1A /* SVWebViewControllerActivityReport.m in Sources */,
45664571
4E6BA2E41A1EE6AF005FD721 /* UIButton+AFNetworking.m in Sources */,
45674572
8EF643C919FF7E2900F7EEB0 /* TopicCommentCell.m in Sources */,
4573+
7EB02FF61B6DAF3800D2166C /* AudioVolumeView.m in Sources */,
45684574
4E8D5D6E1B45400100B70936 /* ZXScanCodeViewController.m in Sources */,
45694575
4ECD72EF1AAD7EC300D69AE1 /* Tweet.m in Sources */,
45704576
8EF643CD19FF7E2900F7EEB0 /* TweetCommentMoreCell.m in Sources */,

Coding_iOS/Util/Audio/AudioManager.m

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
@interface AudioManager () <AVAudioPlayerDelegate, AVAudioRecorderDelegate>
1313

1414
@property (nonatomic, strong) NSString *tmpFile;
15+
@property (nonatomic, strong) NSTimer *meterTimer;
1516

1617
@end
1718

@@ -49,7 +50,7 @@ - (void)play:(NSString *)file validator:(id)validator {
4950

5051
_audioPlayer.delegate = self;
5152
_audioPlayer.volume = 1.0f;
52-
_audioPlayer.meteringEnabled = YES;
53+
// _audioPlayer.meteringEnabled = YES;
5354
[_audioPlayer play];
5455
_validator = validator;
5556
_isPlaying = YES;
@@ -108,6 +109,7 @@ - (void)recordWithValidator:(id)validator {
108109
[_audioRecorder record];
109110
_validator = validator;
110111
_isRecording = YES;
112+
[self startUpdateMeter];
111113

112114
if (_delegate && [_delegate respondsToSelector:@selector(didAudioRecordStarted:)]) {
113115
[_delegate didAudioRecordStarted:self];
@@ -119,6 +121,7 @@ - (void)stopRecord {
119121
}
120122

121123
- (void)stopRecord:(BOOL)successfully {
124+
[self stopUpdateMeter];
122125
NSTimeInterval duration = 0;
123126
if (_audioRecorder) {
124127
duration = _audioRecorder.currentTime;
@@ -134,6 +137,43 @@ - (void)stopRecord:(BOOL)successfully {
134137
}
135138
}
136139

140+
- (void)startUpdateMeter {
141+
self.meterTimer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateMeter) userInfo:nil repeats:YES];
142+
}
143+
144+
- (void)stopUpdateMeter {
145+
if (_meterTimer) {
146+
[_meterTimer invalidate];
147+
self.meterTimer = nil;
148+
}
149+
}
150+
151+
- (void)updateMeter {
152+
[_audioRecorder updateMeters];
153+
154+
double volume; // The linear 0.0 .. 1.0 value we need.
155+
float minDecibels = -80.0f; // Or use -60dB, which I measured in a silent room.
156+
float decibels = [_audioRecorder averagePowerForChannel:0];
157+
158+
if (decibels < minDecibels) {
159+
volume = 0.0f;
160+
} else if (decibels >= 0.0f) {
161+
volume = 1.0f;
162+
} else {
163+
float root = 2.0f;
164+
float minAmp = powf(10.0f, 0.05f * minDecibels);
165+
float inverseAmpRange = 1.0f / (1.0f - minAmp);
166+
float amp = powf(10.0f, 0.05f * decibels);
167+
float adjAmp = (amp - minAmp) * inverseAmpRange;
168+
169+
volume = pow(adjAmp, 1.0f / root);
170+
}
171+
NSLog(@"recording volume:%f", volume);
172+
if (_delegate && [_delegate respondsToSelector:@selector(didAudioRecording:volume:)]) {
173+
[_delegate didAudioRecording:self volume:volume];
174+
}
175+
}
176+
137177
#pragma mark - Dir
138178

139179
+ (NSString *)tmpFile {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// AudioVolumeView.h
3+
// Coding_iOS
4+
//
5+
// Created by sumeng on 8/2/15.
6+
// Copyright (c) 2015 Coding. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
#define kAudioVolumeViewVolumeWidth 2.0f
12+
#define kAudioVolumeViewVolumeMinHeight 3.0f
13+
#define kAudioVolumeViewVolumeMaxHeight 12.0f
14+
#define kAudioVolumeViewVolumePadding 2.0f
15+
#define kAudioVolumeViewVolumeNumber 10
16+
17+
#define kAudioVolumeViewWidth (kAudioVolumeViewVolumeWidth*kAudioVolumeViewVolumeNumber+kAudioVolumeViewVolumePadding*(kAudioVolumeViewVolumeNumber-1))
18+
#define kAudioVolumeViewHeight kAudioVolumeViewVolumeMaxHeight
19+
20+
typedef NS_ENUM(NSInteger, AudioVolumeViewType) {
21+
AudioVolumeViewTypeLeft,
22+
AudioVolumeViewTypeRight
23+
};
24+
25+
@interface AudioVolumeView : UIView
26+
27+
@property (nonatomic, assign) AudioVolumeViewType type;
28+
29+
- (void)addVolume:(double)volume;
30+
- (void)clearVolume;
31+
32+
@end
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// AudioVolumeView.m
3+
// Coding_iOS
4+
//
5+
// Created by sumeng on 8/2/15.
6+
// Copyright (c) 2015 Coding. All rights reserved.
7+
//
8+
9+
#import "AudioVolumeView.h"
10+
11+
@interface AudioVolumeView ()
12+
13+
@property (nonatomic, strong) NSMutableArray *volumeViews;
14+
@property (nonatomic, strong) NSMutableArray *volumes;
15+
16+
@end
17+
18+
@implementation AudioVolumeView
19+
20+
- (instancetype)initWithFrame:(CGRect)frame {
21+
self = [super initWithFrame:frame];
22+
if (self) {
23+
_volumes = [[NSMutableArray alloc] initWithCapacity:kAudioVolumeViewVolumeNumber];
24+
_volumeViews = [[NSMutableArray alloc] initWithCapacity:kAudioVolumeViewVolumeNumber];
25+
for (int i = 0; i < kAudioVolumeViewVolumeNumber; i++) {
26+
[_volumes addObject:@0];
27+
28+
UIView *volumeView = [[UIView alloc] initWithFrame:CGRectMake((kAudioVolumeViewVolumeWidth+kAudioVolumeViewVolumePadding)*i, (self.frame.size.height-kAudioVolumeViewVolumeMinHeight)/2, kAudioVolumeViewVolumeWidth, kAudioVolumeViewVolumeMinHeight)];
29+
volumeView.backgroundColor = [UIColor colorWithRGBHex:0x2faeea];
30+
volumeView.layer.cornerRadius = volumeView.frame.size.width/2;
31+
[self addSubview:volumeView];
32+
[_volumeViews addObject:volumeView];
33+
}
34+
35+
self.type = AudioVolumeViewTypeLeft;
36+
}
37+
return self;
38+
}
39+
40+
- (void)addVolume:(double)volume {
41+
if (_type == AudioVolumeViewTypeRight) {
42+
[_volumes removeLastObject];
43+
[_volumes insertObject:[NSNumber numberWithDouble:volume] atIndex:0];
44+
}
45+
else {
46+
[_volumes removeObjectAtIndex:0];
47+
[_volumes addObject:[NSNumber numberWithDouble:volume]];
48+
}
49+
[self layoutVolumes];
50+
}
51+
52+
- (void)clearVolume {
53+
[_volumes removeAllObjects];
54+
for (int i = 0; i < _volumeViews.count; i++) {
55+
[_volumes addObject:@0];
56+
}
57+
[self layoutVolumes];
58+
}
59+
60+
- (void)layoutVolumes {
61+
for (int i = 0; i < _volumeViews.count; i++) {
62+
UIView *volumeView = _volumeViews[i];
63+
NSNumber *volume = _volumes[i];
64+
[volumeView setHeight:[self heightOfVolume:volume.doubleValue]];
65+
volumeView.center = CGPointMake(volumeView.center.x, self.frame.size.height/2);
66+
}
67+
}
68+
69+
- (CGFloat)heightOfVolume:(double)volume {
70+
return kAudioVolumeViewVolumeMinHeight + (kAudioVolumeViewVolumeMaxHeight - kAudioVolumeViewVolumeMinHeight) * volume;
71+
}
72+
73+
@end

Coding_iOS/Views/UIMessageInputView/UIMessageInputView_Voice.m

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "UIMessageInputView_Voice.h"
1010
#import "AudioRecordView.h"
1111
#import "AudioPlayView.h"
12+
#import "AudioVolumeView.h"
1213

1314
typedef NS_ENUM(NSInteger, UIMessageInputView_VoiceState) {
1415
UIMessageInputView_VoiceStateReady,
@@ -20,6 +21,8 @@ @interface UIMessageInputView_Voice () <AudioRecordViewDelegate>
2021

2122
@property (strong, nonatomic) UILabel *recordTipsLabel;
2223
@property (strong, nonatomic) AudioRecordView *recordView;
24+
@property (strong, nonatomic) AudioVolumeView *volumeLeftView;
25+
@property (strong, nonatomic) AudioVolumeView *volumeRightView;
2326
@property (assign, nonatomic) UIMessageInputView_VoiceState state;
2427
@property (assign, nonatomic) int duration;
2528
@property (strong, nonatomic) NSTimer *timer;
@@ -38,6 +41,16 @@ - (id)initWithFrame:(CGRect)frame {
3841
_recordTipsLabel.font = [UIFont systemFontOfSize:18];
3942
[self addSubview:_recordTipsLabel];
4043

44+
_volumeLeftView = [[AudioVolumeView alloc] initWithFrame:CGRectMake(0, 0, kAudioVolumeViewWidth, kAudioVolumeViewHeight)];
45+
_volumeLeftView.type = AudioVolumeViewTypeLeft;
46+
_volumeLeftView.hidden = YES;
47+
[self addSubview:_volumeLeftView];
48+
49+
_volumeRightView = [[AudioVolumeView alloc] initWithFrame:CGRectMake(0, 0, kAudioVolumeViewWidth, kAudioVolumeViewHeight)];
50+
_volumeRightView.type = AudioVolumeViewTypeRight;
51+
_volumeRightView.hidden = YES;
52+
[self addSubview:_volumeRightView];
53+
4154
_recordView = [[AudioRecordView alloc] initWithFrame:CGRectMake((self.frame.size.width - 88) / 2, 62, 88, 88)];
4255
_recordView.delegate = self;
4356
[self addSubview:_recordView];
@@ -70,6 +83,8 @@ - (void)setState:(UIMessageInputView_VoiceState)state {
7083
case UIMessageInputView_VoiceStateReady:
7184
_recordTipsLabel.textColor = [UIColor colorWithRGBHex:0x999999];
7285
_recordTipsLabel.text = @"按住说话";
86+
_volumeLeftView.hidden = YES;
87+
_volumeRightView.hidden = YES;
7388
break;
7489
case UIMessageInputView_VoiceStateRecording:
7590
_recordTipsLabel.textColor = [UIColor colorWithRGBHex:0x2faeea];
@@ -78,12 +93,21 @@ - (void)setState:(UIMessageInputView_VoiceState)state {
7893
case UIMessageInputView_VoiceStateCancel:
7994
_recordTipsLabel.textColor = [UIColor colorWithRGBHex:0x999999];
8095
_recordTipsLabel.text = @"松开取消";
96+
_volumeLeftView.hidden = YES;
97+
_volumeRightView.hidden = YES;
8198
break;
8299
default:
83100
break;
84101
}
85102
[_recordTipsLabel sizeToFit];
86103
_recordTipsLabel.center = CGPointMake(self.frame.size.width/2, 20);
104+
105+
if (state == UIMessageInputView_VoiceStateRecording) {
106+
_volumeLeftView.center = CGPointMake(_recordTipsLabel.frame.origin.x - _volumeLeftView.frame.size.width/2 - 10, _recordTipsLabel.center.y);
107+
_volumeLeftView.hidden = NO;
108+
_volumeRightView.center = CGPointMake(_recordTipsLabel.frame.origin.x + _recordTipsLabel.frame.size.width + _recordTipsLabel.frame.size.width/2 + 10, _recordTipsLabel.center.y);
109+
_volumeRightView.hidden = NO;
110+
}
87111
}
88112

89113
#pragma mark - RecordTimer
@@ -117,6 +141,8 @@ - (NSString *)formattedTime:(int)duration {
117141
#pragma mark - AudioRecordViewDelegate
118142

119143
- (void)recordViewRecordStarted:(AudioRecordView *)recordView {
144+
[_volumeLeftView clearVolume];
145+
[_volumeRightView clearVolume];
120146
self.state = UIMessageInputView_VoiceStateRecording;
121147
[self startTimer];
122148
}
@@ -138,7 +164,8 @@ - (void)recordView:(AudioRecordView *)recordView touchStateChanged:(AudioRecordV
138164
}
139165

140166
- (void)recordView:(AudioRecordView *)recordView volume:(double)volume {
141-
167+
[_volumeLeftView addVolume:volume];
168+
[_volumeRightView addVolume:volume];
142169
}
143170

144171
@end

0 commit comments

Comments
 (0)