@@ -21,13 +21,13 @@ @implementation RNAudioRecorderPlayer {
21
21
AVAudioPlayer *audioPlayer;
22
22
NSTimer *recordTimer;
23
23
NSTimer *playTimer;
24
+ BOOL _meteringEnabled;
24
25
}
25
26
double subscriptionDuration = 0.1 ;
26
27
27
28
- (void )audioPlayerDidFinishPlaying : (AVAudioPlayer *)player successfully : (BOOL )flag {
28
29
NSLog (@" audioPlayerDidFinishPlaying" );
29
30
NSNumber *duration = [NSNumber numberWithDouble: audioPlayer.duration * 1000 ];
30
- NSNumber *currentTime = [NSNumber numberWithDouble: audioPlayer.duration * 1000 ];
31
31
32
32
// Send last event then finish it.
33
33
// NSString* status = [NSString stringWithFormat:@"{\"duration\": \"%@\", \"current_position\": \"%@\"}", [duration stringValue], [currentTime stringValue]];
@@ -46,8 +46,15 @@ - (void)updateRecorderProgress:(NSTimer*) timer
46
46
{
47
47
NSNumber *currentTime = [NSNumber numberWithDouble: audioRecorder.currentTime * 1000 ];
48
48
// NSString* status = [NSString stringWithFormat:@"{\"current_position\": \"%@\"}", [currentTime stringValue]];
49
+ NSNumber *currentMetering = [NSNumber numberWithDouble: 0 ];
50
+ if (_meteringEnabled) {
51
+ [audioRecorder updateMeters ];
52
+ currentMetering = [NSNumber numberWithDouble: [audioRecorder averagePowerForChannel: 0 ]];
53
+ }
54
+
49
55
NSDictionary *status = @{
50
56
@" current_position" : [currentTime stringValue ],
57
+ @" current_metering" : [currentMetering stringValue ],
51
58
};
52
59
[self sendEventWithName: @" rn-recordback" body: status];
53
60
}
@@ -64,7 +71,7 @@ - (void)updateProgress:(NSTimer*) timer
64
71
[audioPlayer stop ];
65
72
return ;
66
73
}
67
-
74
+
68
75
// NSString* status = [NSString stringWithFormat:@"{\"duration\": \"%@\", \"current_position\": \"%@\"}", [duration stringValue], [currentTime stringValue]];
69
76
NSDictionary *status = @{
70
77
@" duration" : [duration stringValue ],
@@ -116,6 +123,7 @@ - (dispatch_queue_t)methodQueue
116
123
}
117
124
118
125
RCT_EXPORT_METHOD (startRecorder:(NSString *)path
126
+ meteringEnabled:(BOOL )meteringEnabled
119
127
audioSets: (NSDictionary *)audioSets
120
128
resolve:(RCTPromiseResolveBlock)resolve
121
129
reject:(RCTPromiseRejectBlock)reject) {
@@ -125,6 +133,7 @@ - (dispatch_queue_t)methodQueue
125
133
NSNumber *numberOfChannel = [RCTConvert NSNumber: audioSets[@" AVNumberOfChannelsKeyIOS" ]];
126
134
NSNumber *avFormat;
127
135
NSNumber *audioQuality = [RCTConvert NSNumber: audioSets[@" AVEncoderAudioQualityKeyIOS" ]];
136
+ _meteringEnabled = meteringEnabled;
128
137
129
138
if ([path isEqualToString: @" DEFAULT" ]) {
130
139
audioFileURL = [NSURL fileURLWithPath: [GetDirectoryOfType_Sound (NSCachesDirectory) stringByAppendingString: @" sound.m4a" ]];
@@ -196,11 +205,12 @@ - (dispatch_queue_t)methodQueue
196
205
initWithURL: audioFileURL
197
206
settings: audioSettings
198
207
error: nil ];
199
-
208
+ audioRecorder.meteringEnabled = _meteringEnabled;
209
+
200
210
[audioRecorder setDelegate: self ];
201
211
[audioRecorder record ];
202
212
[self startRecorderTimer ];
203
-
213
+
204
214
NSString *filePath = self->audioFileURL .absoluteString ;
205
215
resolve (filePath);
206
216
}
@@ -271,7 +281,7 @@ - (dispatch_queue_t)methodQueue
271
281
audioFileURL = [NSURL URLWithString: path];
272
282
}
273
283
}
274
-
284
+
275
285
NSLog (@" Error %@ " ,error);
276
286
277
287
if (!audioPlayer) {
@@ -333,7 +343,7 @@ - (dispatch_queue_t)methodQueue
333
343
if (playTimer != nil ) {
334
344
[playTimer invalidate ];
335
345
playTimer = nil ;
336
- }
346
+ }
337
347
resolve (@" pause play" );
338
348
} else {
339
349
reject (@" audioPlayer pause" , @" audioPlayer is not playing" , nil );
0 commit comments