Struggling with unreliable Event trigger from objective-c to react native

24 views Asked by At

I am building a React Native application which records an audio in the first place. Which works quite smoothly except the case when my recording gets interrupted (e.g. music starts, phone call.)

While recording and the phone is on lockscreen I accidentally start some music or a phone call occurs. The listener for interruption I built stops the recording and sends an event to React Native to handle the state and sending a push notification e.g..

This works, but pretty unreliable. I guess it's because everything happens in background. Pretty happy about any help!

I built a listener for interruptions which triggers the following objective-c function:

- (void)audioSessionInterruptionHandler:(NSNotification *)notification {
    NSDictionary *userInfo = notification.userInfo;
    NSInteger interruptionType = [userInfo[AVAudioSessionInterruptionTypeKey] integerValue];
    
    if (interruptionType == AVAudioSessionInterruptionTypeBegan) {
        NSLog(@"INTERRUPTION HAPPENED");
        [self pauseRecording];
    } else if (interruptionType == AVAudioSessionInterruptionTypeEnded) {
        NSLog(@"INTERRUPTION ENDED");
        [self resumeRecording];
    }
}

this works solidly!

As the interruption happens I pause the current recording and trigger an Event (RCTEventEmitter) as followed:

RCT_EXPORT_METHOD(pauseRecording)
{
    if (_audioRecorder.isRecording) {
        [_audioRecorder pause];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self sendEventWithName:AudioRecorderEventStatus body:@{
                @"isRecording": @(NO),
                @"sendPushNotification": @(YES),
            }];
        });
    }
}

AudioRecorderEventStatus is one of my registered events:

- (NSArray<NSString *> *)supportedEvents {
    return @[AudioRecorderEventProgress, AudioRecorderEventStatus];
}

In my native code:

var AudioRecorderManager = NativeModules.AudioRecorderManager;
const EventEmitter = new NativeEventEmitter(AudioRecorderManager);

var AudioRecorder = {
  prepareRecordingAtPath: function(path, options) {
    if (this.statusSubscription) this.statusSubscription.remove();
    this.statusSubscription = EventEmitter.addListener('recordingStatus',
      (data) => {
        console.log('recordingStatus', data);
        if (this.onStatus) {
          console.log('recordingStatus INSIDE', data);
          this.onStatus(data);
        }
      }
    );

...

and here occurs the issue! my second log console.log('recordingStatus INSIDE', data) does not always gets triggered as this.onStatus is undefined. But it's absolutely random when it works and when not.

0

There are 0 answers