diff --git a/ios/App/Shared/player/AudioPlayer.swift b/ios/App/Shared/player/AudioPlayer.swift index 304181c0..f188b268 100644 --- a/ios/App/Shared/player/AudioPlayer.swift +++ b/ios/App/Shared/player/AudioPlayer.swift @@ -71,6 +71,7 @@ class AudioPlayer: NSObject { } // Listen to player events + self.setupInteruptionNotification() self.audioPlayer.addObserver(self, forKeyPath: #keyPath(AVPlayer.rate), options: .new, context: &playerContext) self.audioPlayer.addObserver(self, forKeyPath: #keyPath(AVPlayer.currentItem), options: .new, context: &playerContext) @@ -119,6 +120,7 @@ class AudioPlayer: NSObject { print(error) } + self.removeInteruptionNotification() DispatchQueue.runOnMainQueue { UIApplication.shared.endReceivingRemoteControlEvents() } @@ -146,6 +148,14 @@ class AudioPlayer: NSObject { return 0 } + private func setupInteruptionNotification() { + NotificationCenter.default.addObserver(self, selector: #selector(handleInteruption), name: AVAudioSession.interruptionNotification, object: AVAudioSession.sharedInstance()) + } + + private func removeInteruptionNotification() { + NotificationCenter.default.removeObserver(self, name: AVAudioSession.interruptionNotification, object: AVAudioSession.sharedInstance()) + } + private func setupTimeObserver() { // Time observer should be configured on the main queue DispatchQueue.runOnMainQueue { @@ -588,6 +598,25 @@ class AudioPlayer: NSObject { } } + // MARK: - iOS audio interupt notifications + @objc private func handleInteruption(notification: Notification) { + guard let userInfo = notification.userInfo, + let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt, + let type = AVAudioSession.InterruptionType(rawValue: typeValue) else { + return + } + + switch type { + case .ended: + guard let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt else { return } + let options = AVAudioSession.InterruptionOptions(rawValue: optionsValue) + if options.contains(.shouldResume) { + self.play(allowSeekBack: true) + } + default: () + } + } + // MARK: - Now playing private func setupRemoteTransportControls() { DispatchQueue.runOnMainQueue {