Skip to content

Commit

Permalink
feature: handling iOS recording interruption events (#601)
Browse files Browse the repository at this point in the history
Add a mechanism to handle recording interruptions caused by iOS external applications.

Related issues: [#170](#170) [#583](#583)
  • Loading branch information
pipi32167 authored Mar 26, 2024
1 parent 522309e commit 38c7f58
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions ios/RNAudioRecorderPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ class RNAudioRecorderPlayer: RCTEventEmitter, AVAudioRecorderDelegate {
var playTimer: Timer?
var timeObserverToken: Any?

override init() {
super.init()
NotificationCenter.default.addObserver(self, selector: #selector(handleAudioSessionInterruption(_:)), name: AVAudioSession.interruptionNotification, object: AVAudioSession.sharedInstance())
}

deinit {
NotificationCenter.default.removeObserver(self)
}

override static func requiresMainQueueSetup() -> Bool {
return true
}
Expand Down Expand Up @@ -138,6 +147,26 @@ class RNAudioRecorderPlayer: RCTEventEmitter, AVAudioRecorderDelegate {
subscriptionDuration = duration
}

// handle interrupt events
@objc
func handleAudioSessionInterruption(_ notification: Notification) {
guard let userInfo = notification.userInfo,
let interruptionType = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt else {
return
}

switch interruptionType {
case AVAudioSession.InterruptionType.began.rawValue:
pauseRecorder { _ in } rejecter: { _, _, _ in }
break
case AVAudioSession.InterruptionType.ended.rawValue:
resumeRecorder { _ in } rejecter: { _, _, _ in }
break
default:
break
}
}

/********** Player **********/

@objc(startRecorder:audioSets:meteringEnabled:resolve:reject:)
Expand Down

0 comments on commit 38c7f58

Please sign in to comment.