AVPlayer disconnects Airplay

On an Apple TV, if you have an app that triggers a video playing in AVPlayer while another device is air playing to the Apple TV. The device gets disconnected from Airplay and the video plays instead. There doesn’t seem to be a way to sense that Airplay is happening or not happening from the Apple TV side programmatically or a way to prevent the behavior. Is this intentional? It would seem to make more sense to have Airplay take priority or push the app to the background. This is true with both swift and swiftUI.

I tried detecting when Airplay begins and ends, but no api seems to give this information. The app doesn’t know something is Airplaying, so continues to work and when it trigger AVPlayer to play, it disconnects whatever is airplaying.

let videoController = AVPlayerViewController()
let player = AVPlayer(url: [Some video url])
videoController.player = player

Make sure to start airplaying between starting the app and the video playing.

Timer.scheduledTimer(withTimeInterval: 20, repeats: true) {  _ in
      present(videoController, animated: false) { [weak self] in
            guard let self else { return }
            player.seek(to: .zero)
            player.play()
      }
}

  • Please provide enough code so others can better understand or reproduce the problem.

    – 
    Bot

Leave a Comment