@@ -21,13 +21,13 @@ @implementation RNAudioRecorderPlayer {
2121 AVAudioPlayer *audioPlayer;
2222 NSTimer *recordTimer;
2323 NSTimer *playTimer;
24+ BOOL _meteringEnabled;
2425}
2526double subscriptionDuration = 0.1 ;
2627
2728- (void )audioPlayerDidFinishPlaying : (AVAudioPlayer *)player successfully : (BOOL )flag {
2829 NSLog (@" audioPlayerDidFinishPlaying" );
2930 NSNumber *duration = [NSNumber numberWithDouble: audioPlayer.duration * 1000 ];
30- NSNumber *currentTime = [NSNumber numberWithDouble: audioPlayer.duration * 1000 ];
3131
3232 // Send last event then finish it.
3333 // NSString* status = [NSString stringWithFormat:@"{\"duration\": \"%@\", \"current_position\": \"%@\"}", [duration stringValue], [currentTime stringValue]];
@@ -46,8 +46,15 @@ - (void)updateRecorderProgress:(NSTimer*) timer
4646{
4747 NSNumber *currentTime = [NSNumber numberWithDouble: audioRecorder.currentTime * 1000 ];
4848 // 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+
4955 NSDictionary *status = @{
5056 @" current_position" : [currentTime stringValue ],
57+ @" current_metering" : [currentMetering stringValue ],
5158 };
5259 [self sendEventWithName: @" rn-recordback" body: status];
5360}
@@ -64,7 +71,7 @@ - (void)updateProgress:(NSTimer*) timer
6471 [audioPlayer stop ];
6572 return ;
6673 }
67-
74+
6875 // NSString* status = [NSString stringWithFormat:@"{\"duration\": \"%@\", \"current_position\": \"%@\"}", [duration stringValue], [currentTime stringValue]];
6976 NSDictionary *status = @{
7077 @" duration" : [duration stringValue ],
@@ -116,6 +123,7 @@ - (dispatch_queue_t)methodQueue
116123}
117124
118125RCT_EXPORT_METHOD (startRecorder:(NSString *)path
126+ meteringEnabled:(BOOL )meteringEnabled
119127 audioSets: (NSDictionary *)audioSets
120128 resolve:(RCTPromiseResolveBlock)resolve
121129 reject:(RCTPromiseRejectBlock)reject) {
@@ -125,6 +133,7 @@ - (dispatch_queue_t)methodQueue
125133 NSNumber *numberOfChannel = [RCTConvert NSNumber: audioSets[@" AVNumberOfChannelsKeyIOS" ]];
126134 NSNumber *avFormat;
127135 NSNumber *audioQuality = [RCTConvert NSNumber: audioSets[@" AVEncoderAudioQualityKeyIOS" ]];
136+ _meteringEnabled = meteringEnabled;
128137
129138 if ([path isEqualToString: @" DEFAULT" ]) {
130139 audioFileURL = [NSURL fileURLWithPath: [GetDirectoryOfType_Sound (NSCachesDirectory) stringByAppendingString: @" sound.m4a" ]];
@@ -196,11 +205,12 @@ - (dispatch_queue_t)methodQueue
196205 initWithURL: audioFileURL
197206 settings: audioSettings
198207 error: nil ];
199-
208+ audioRecorder.meteringEnabled = _meteringEnabled;
209+
200210 [audioRecorder setDelegate: self ];
201211 [audioRecorder record ];
202212 [self startRecorderTimer ];
203-
213+
204214 NSString *filePath = self->audioFileURL .absoluteString ;
205215 resolve (filePath);
206216}
@@ -271,7 +281,7 @@ - (dispatch_queue_t)methodQueue
271281 audioFileURL = [NSURL URLWithString: path];
272282 }
273283 }
274-
284+
275285 NSLog (@" Error %@ " ,error);
276286
277287 if (!audioPlayer) {
@@ -333,7 +343,7 @@ - (dispatch_queue_t)methodQueue
333343 if (playTimer != nil ) {
334344 [playTimer invalidate ];
335345 playTimer = nil ;
336- }
346+ }
337347 resolve (@" pause play" );
338348 } else {
339349 reject (@" audioPlayer pause" , @" audioPlayer is not playing" , nil );
0 commit comments