AirPlay Reference (iOS)
Learn how to listen for AirPlay events and configure receiver interactions
The iOS SDK supports streaming to an AirPlay receiver, such as a nearby Apple TV or Mac. The iOS SDK offers a convenient way to listen for and respond to AirPlay events, and to configure how the player interacts with AirPlay receivers.
Listen for AirPlay events
When the iOS device connects to or disconnects from an AirPlay receiver, the SDK will fire the jwplayer(_:airPlayStatusChanged:)
method of its JWAirPlayDelegate
. The JWAirPlayDelegate
is like all the other delegates.
// Subclass of JWPlayerViewController
class PlayerViewController: JWPlayerViewController {
// other codeβ¦
override func jwplayer(_ player: JWPlayer, airPlayStatusChanged status: JWAirPlayStatus) {
super.jwplayer(player, airPlayStatusChanged: status)
// Handle the event here.
}
}
// Subclass of JWPlayerObjViewController
@implementation PlayerViewController
// other codeβ¦
- (void)jwplayer:(id<JWPlayer>)player airPlayStatusChanged:(enum JWAirPlayStatus)status {
[super jwplayer:player airPlayStatusChanged:status];
// Handle the event here.
}
A
JWPlayerViewController
and subclass will handle the creation and displaying of an AirPlay button.When implementing a custom UI over a
JWPlayerView
, you will need to add an AirPlay button to your UI, using Appleβs API.private func createAirPlayButton() -> UIView { let toolBarButtonSize = MyUIConstants.barButtonItemSize let buttonFrame = CGRect(x: 0, y: 0, width: toolBarButtonSize.width, height: toolBarButtonSize.height) let button = AVRoutePickerView(frame: buttonFrame) button.accessibilityTraits = .button return button }
In this case, the
JWAirPlayDelegate
you have assigned to theJWPlayer
object will still receive AirPlay status events.
Configure external playback settings
The iOS SDK also provides the JWExternalPlaybackSettings
object, which allows you to configure how the player interacts with external playback devices, such as AirPlay receivers.
Each property of the JWExternalPlaybackSettings
object correlates to a setting in AVPlayer
with its default value.
JWExternalPlaybackSettings Property | AVPlayer Setting |
---|---|
playbackEnabled | allowsExternalPlayback |
usesExternalPlaybackWhileExternalScreenIsActive | usesExternalPlaybackWhileExternalScreenIsActive |
videoGravity | externalPlaybackVideoGravity |
In the following example, playback is enabled on external devices (.playbackEnabled(true)
) and the entire video image is fitted into the screen in a letterbox format (.resizeAspectFit
).
do {
// Create a player item
let item = try JWPlayerItemBuilder()
.file(URL(string: "video_url")!)
.build()
// Configure external playback settings
let myExternalPlaybackSettings = try JWExternalPlaybackSettingsBuilder()
.playbackEnabled(true)
.videoGravity(.resizeAspectFit)
.build()
// Configure the player
let config = try JWPlayerConfigurationBuilder()
.playlist([item])
.externalPlaybackSettings(myExternalPlaybackSettings)
.build()
player.configurePlayer(with: config)
} catch {
// Handle errors
}
Updated 9 months ago