title: Audio session .ended isn't called with two AVPlayers
body: <p>Here are steps to reproduce:</p>
  <ol>
  <li>Activate <code>AVAudioSession</code> with <code>.playback</code> category.</li>
  <li>Register for <code>AVAudioSession.interruptionNotification</code></li>
  <li>Create two <code>AVPlayer</code>s and start them</li>
  <li>Interrupt playback by calling Siri/receiving a call by Skype, Cellular and etc.</li>
  </ol>
  <p><strong>Expected behavior:</strong>
  Receiving notification of the audio session interruption with <code>.began</code> state at the start and <code>.ended</code> at the end. Also, as a side effect, Siri doesn't respond to commands.</p>
  <p><strong>Real behavior:</strong>
  Only <code>.began</code> notification is called.</p>
  <p>To bring back <code>.ended</code> notification (which is used to continue playback) remove one player.</p>
  <p><strong>Question:</strong> how to handle the audio session interruption with more than 1 AVPlayer running?</p>
  <p>Here I created a simple demo project: <a href="https://github.com/denis-obukhov/AVAudioSessionBug" rel="nofollow noreferrer">https://github.com/denis-obukhov/AVAudioSessionBug</a></p>
  <p>Tested on iOS 14.4</p>
  <pre><code>import UIKit
  import AVFoundation
  
  class ViewController: UIViewController {
    private let player1: AVPlayer? = {
      $0.volume = 0.5
      return $0
    }(AVPlayer())
  
    private let player2: AVPlayer? = {
      $0.volume = 0.5
      return $0 // return nil for any player to bring back .ended interruption notification
    }(AVPlayer())
  
    override func viewDidLoad() {
      super.viewDidLoad()
      registerObservers()
      startAudioSession()
    }
  
    override func viewDidAppear(_ animated: Bool) {
      super.viewDidAppear(animated)
      player1?.replaceCurrentItem(with: makePlayerItem(named: &quot;music1&quot;))
      player2?.replaceCurrentItem(with: makePlayerItem(named: &quot;music2&quot;))
      [player1, player2].forEach { $0?.play() }
    }
  
    private func makePlayerItem(named name: String) -&gt; AVPlayerItem {
      let fileURL = Bundle.main.url(
        forResource: name,
        withExtension: &quot;mp3&quot;
      )!
      return AVPlayerItem(url: fileURL)
    }
  
    private func registerObservers() {
      NotificationCenter.default.addObserver(
        self, selector: #selector(handleInterruption(_:)),
        name: AVAudioSession.interruptionNotification,
        object: nil
      )
    }
  
    private func startAudioSession() {
      try? AVAudioSession.sharedInstance().setCategory(.playback)
      try? AVAudioSession.sharedInstance().setActive(true)
    }
  
    @objc private func handleInterruption(_ notification: Notification) {
      print(&quot;GOT INTERRUPTION&quot;)
      guard
        let userInfo = notification.userInfo,
        let typeValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
        let type = AVAudioSession.InterruptionType(rawValue: typeValue)
      else {
        return
      }
  
      switch type {
      case .began:
        print(&quot;Interruption BEGAN&quot;)
        [player1, player2].forEach { $0?.pause() }
      case .ended:
        // This part isn't called if more than 1 player is playing
        print(&quot;Interruption ENDED&quot;)
        [player1, player2].forEach { $0?.play() }
      @unknown default:
        print(&quot;Unknown value&quot;)
      }
    }
  }
  </code></pre>