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:
- Start playing to airplay with the first player
- Dismiss the player (while the airplay is still connected)
- 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:
- Deactivate the
AudioSession
withtry session.setActive(false)
try session.setCategory(.playAndRecord, options: [.defaultToSpeaker])
- Change category to
.playAndRecord
andsession.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