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.

The AirPlay button is located at the top-right corner of the player.

AirPlay button located at the top-right corner of the player



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 the JWPlayer 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 PropertyAVPlayer Setting
playbackEnabledallowsExternalPlayback
usesExternalPlaybackWhileExternalScreenIsActiveusesExternalPlaybackWhileExternalScreenIsActive
videoGravityexternalPlaybackVideoGravity



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
}


© 2007-2024 Longtail Ad Solutions, Inc.