diff --git a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioSession.swift b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioSession.swift index 86b9cb77e..afa1ef8ff 100644 --- a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioSession.swift +++ b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioSession.swift @@ -103,6 +103,7 @@ public class AudioSession: NSObject, ObservableObject, AVAudioPlayerDelegate { public func startAudio() { state = .loading + setupNotifications() let pageId = item!.unwrappedID @@ -361,4 +362,37 @@ public class AudioSession: NSObject, ObservableObject, AVAudioPlayerDelegate { player.currentTime = 0 } } + + func setupNotifications() { + NotificationCenter.default.removeObserver(self, name: AVAudioSession.interruptionNotification, object: AVAudioSession.sharedInstance()) + NotificationCenter.default.addObserver(self, + selector: #selector(handleInterruption), + name: AVAudioSession.interruptionNotification, + object: AVAudioSession.sharedInstance()) + } + + @objc func handleInterruption(notification: Notification) { + guard let userInfo = notification.userInfo, + let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt, + let type = AVAudioSession.InterruptionType(rawValue: typeValue) + else { + return + } + + // Switch over the interruption type. + switch type { + case .began: + // An interruption began. Update the UI as necessary. + pause() + case .ended: + // An interruption ended. Resume playback, if appropriate. + + guard let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt else { return } + let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue) + if options.contains(.shouldResume) { + unpause() + } else {} + default: () + } + } }