The application plays an audio file (in active or in the background). An incoming call arrives, the system pauses the playback. How to automatically start playing audio again immediately after the end of the call?

UPD: epplovskie documents on this topic:

https://developer.apple.com/documentation/avfoundation/avaudiosession#//apple_ref/doc/uid/TP40008240-CH1-DontLinkElementID__

https://developer.apple.com/documentation/avfoundation/avaudiosession/responding_to_audio_session_interruptions

From them such code turns out:

private func setupAudioSession() { do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers) try AVAudioSession.sharedInstance().setActive(true) setupAudioNotifications() } catch { print(error) } } private func setupAudioNotifications() { NotificationCenter.default.addObserver(self, selector: #selector(handleInterruption), name: .AVAudioSessionInterruption, object: nil) } @objc func handleInterruption(notification: Notification) { guard let userInfo = notification.userInfo, let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt, let type = AVAudioSessionInterruptionType(rawValue: typeValue) else { return } if type == .began { // при звонке .began срабатывает всегда // Interruption began, take appropriate actions Player.shared.stop() } else if type == .ended { // а вот когда звонок заканчивается (любой из сторон), .ended не срабатывает if let optionsValue = userInfo[AVAudioSessionInterruptionOptionKey] as? UInt { let options = AVAudioSessionInterruptionOptions(rawValue: optionsValue) if options.contains(.shouldResume) { // Interruption Ended - playback should resume Player.shared.start() } else { // Interruption Ended - playback should NOT resume } } } } 

but, if the .began event always fires and the player stops, then the .ended event for some reason does not work and the player’s command to continue playback does not go away.

Testing takes place as follows: incoming from another phone comes in (at the same time .began is triggered), after a few beeps on the outgoing phone, the call is pressed to end (in this case, the call to handleInterruption does not already occur)

    0