Implementing AirPlay Streaming from a Mobile Application
AirPlay is an Apple protocol for wireless transmission of audio and video to Apple TV, HomePod, AirPlay 2-compatible televisions and speakers. If an application plays media through AVPlayer, basic AirPlay works without additional code — but only basic functionality.
What Works Automatically vs. What Requires Configuration
AVPlayer automatically supports AirPlay when player.allowsExternalPlayback = true (the default value is true). Users see the AirPlay icon in the system Control Center, select a device, and video switches automatically.
What doesn't work without code:
- AirPlay button directly in the application interface
- Handling playback device changes
- Control over what is transmitted (routing video to TV, audio to HomePod)
AVRoutePickerView: AirPlay Button in UI
AVRoutePickerView is a system component that displays the standard list of available devices:
let routePickerView = AVRoutePickerView()
routePickerView.tintColor = .white
routePickerView.activeTintColor = .systemBlue
routePickerView.prioritizesVideoDevices = true // shows AirPlay-TV above speakers
Add directly to the player UI — no additional permissions required; the picker is system-provided.
Monitoring Playback Route
When connecting to an AirPlay device, the UI must adapt: hide the fullscreen button (video is already on TV), show an indicator "Transmitting to...".
NotificationCenter.default.addObserver(
forName: AVAudioSession.routeChangeNotification,
object: nil, queue: .main
) { notification in
guard let reason = notification.userInfo?[AVAudioSessionRouteChangeReasonKey] as? UInt,
let changeReason = AVAudioSession.RouteChangeReason(rawValue: reason) else { return }
switch changeReason {
case .newDeviceAvailable:
let outputs = AVAudioSession.sharedInstance().currentRoute.outputs
let isAirPlay = outputs.contains { $0.portType == .airPlay }
self.updateUIForAirPlay(isAirPlay)
case .oldDeviceUnavailable:
self.updateUIForAirPlay(false)
default: break
}
}
player.isExternalPlaybackActive is a flag indicating that video is currently being transmitted to an external screen. When true, the main phone screen shows an "Playing on Apple TV" placeholder instead of a black screen.
AirPlay 2 and Multi-room Audio
AirPlay 2 allows audio playback simultaneously on multiple devices. To support it in an application: AVAudioSession category .playback or .playAndRecord — that's all that's needed. The system handles multi-routing automatically.
Limitations
AirPlay is only available on iOS and macOS. For Android applications, the equivalent is Chromecast (Google Cast SDK). For cross-platform support, we implement both protocols.
Video with DRM (FairPlay) is transmitted via AirPlay only to devices with FairPlay support. Apple TV supports it; most third-party AirPlay devices do not. Users see a playback error — this is an Apple limitation that cannot be bypassed.
AirPlay for Audio: Separate Route
When transmitting video to an AirPlay device, audio can remain on the phone or go with the video — this depends on AVAudioSession settings. For correct behavior (audio and video together on the AirPlay device): AVAudioSession.Category.playback without allowBluetoothA2DP. If you need to keep audio on the phone while video is on TV — set AVAudioSession.setCategory(.playback, options: [.allowAirPlay]) and configure preferredInput.
Testing Without Physical Apple TV
Network.framework allows emulating a Bonjour service in the simulator for basic testing. However, comprehensive AirPlay testing requires a physical device — Apple TV, HomePod, or an AirPlay 2-compatible television. Automated AirPlay functionality testing in CI/CD is not possible — only manual verification.
Timeline
AirPlay button in UI + route monitoring + correct placeholder display — 1–2 days.







