4

I have a main video that plays through AVPlayerViewController that should be played through Airplay to e.g. the AppleTV. This is how I set up the view controller:

let player = AVPlayer(url: url)
let controller = AVPlayerViewController()
controller.player = player

And here is the setup of the AudioSession:

try session.setCategory(.playback)
try session.setActive(true)

Background mode capability for Audio, AirPlay and Picture in Picture is added.

Then I have another video, which should never be played with AirPlay. In the actual app this video would be played automatically when screen is visited in AVPlayerLayer. But for simplicity I am using another AVPlayerViewController in this example. I have seen the WWDC about Airplay 2 where they recommend setting player.allowsExternalPlayback = false for these exact circumstance.

This is how I setup the player that should not play to AirPlay:

let player = AVPlayer(url: url)
player.allowsExternalPlayback = false

let controller = AVPlayerViewController()
controller.player = player

And this kind of works. However, if you:

  1. Start playing to airplay with the first player
  2. Dismiss the player (while the airplay is still connected)
  3. Start playing the second player (the one with the external playback disabled)

The video is playing on device but audio is playing on the external device.

What I have tried:

  1. Deactivate the AudioSession with try session.setActive(false)
  2. try session.setCategory(.playAndRecord, options: [.defaultToSpeaker])
  3. Change category to .playAndRecord and session.overrideOutputAudioPort(.speaker)

With no luck.

I was able to change the session to multiRoute category which will disconnect from Airplay but it will also disconnect from any headphones and will disable volume control for some reason.

I should also add that it will play audio disregarding that the player.isMuted = true.

I have prepared an example project here: https://github.com/tkohout/AirplayTest. On a third tab there's also a Settings VC where you can play around with the Audio Session settings.

Does anybody encounter similar problem? Is this solvable or is it just a bug / feature of AirPlay? I am testing on devices with iOS 15.

Thanks a lot Tomas Kohout

Tomáš Kohout
  • 755
  • 6
  • 15
  • Hi @Tomáš I am in the similar situation if enabled allowsExternalPlayback = false then only video plays as an audio, if I set it true it does not play anything on TV. Finally how did you solve this problem. ( I mean streaming video on TV via AirPlay Local iPhone Photo Gallery Videos) Looking forward to hearing from you soon. Have a great day! Thank You In Advance! – Pias May 07 '23 at 11:09

1 Answers1

1

For anybody facing the same issue - we've found a partial workaround. When the second player is about to autoplay we will first check whether airplay is not connected with the following code:

var isAirplayActive: Bool {
    let route = AVAudioSession.sharedInstance().currentRoute
    return route.outputs.contains { $0.portType == .airPlay }
}

That way the airplay playback will not at least start without user interaction.

Tomáš Kohout
  • 755
  • 6
  • 15