Add an AirPlay button to an app

Learn how to add an AirPlay button with the iOS SDK

The JW Player iOS SDK supports playback via AirPlay. In order to cast via AirPlay, your Apple device must be connected to an AirPlay receiver, for example, an Apple TV. Connecting to an AirPlay receiver can be done in the Control Center or by adding an AirPlay button in your app.

When casting, be aware that VAST ads will not play and WebVTT captions will only appear on the mobile device.

App screen with an AirPlay button in the navigation bar

App screen with an AirPlay button in the navigation bar

Use the following code to place an AirPlay button in the navigation bar of the ViewController in which this method is embedded.

- (void)setupAirPlayButton {
	UIView *buttonView = nil;
	CGRect buttonFrame = CGRectMake(0, 0, 44, 44);
	
	// It's highly recommended to use the AVRoutePickerView in order to avoid AirPlay issues after iOS 11.
	if (@available(iOS 11.0, *)) {
		AVRoutePickerView *airplayButton = [[AVRoutePickerView alloc] initWithFrame:buttonFrame];
		airplayButton.activeTintColor = [UIColor blueColor];
		airplayButton.tintColor = [UIColor grayColor];
		buttonView = airplayButton;
	} else {
		// If you still support previous iOS versions, you can use MPVolumeView
		MPVolumeView *airplayButton = [[MPVolumeView alloc] initWithFrame:buttonFrame];
		airplayButton.showsVolumeSlider = NO;
		buttonView = airplayButton;
	}
	
	// If there are no AirPlay devices available, the button will not be displayed.
	UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
	[self.navigationItem setRightBarButtonItem:buttonItem animated:YES];
}
func setupAirPlayButton() {
    var buttonView: UIView ? = nil
    let buttonFrame = CGRect(x: 0, y: 0, width: 44, height: 44)

    // It's highly recommended to use the AVRoutePickerView in order to avoid AirPlay issues after iOS 11.
    if# available(iOS 11.0, *) {
        let airplayButton = AVRoutePickerView(frame: buttonFrame)
        airplayButton.activeTintColor = UIColor.blue
        airplayButton.tintColor = UIColor.gray
        buttonView = airplayButton
    } else {
        // If you still support previous iOS versions you can use MPVolumeView
        let airplayButton = MPVolumeView(frame: buttonFrame)
        airplayButton.showsVolumeSlider = false
        buttonView = airplayButton
    }

    // If there are no AirPlay devices available, the button will not be displayed.
    let buttonItem = UIBarButtonItem(customView: buttonView!)
    self.navigationItem.setRightBarButton(buttonItem, animated: true)
}


The JWPlayerController API controls the playback of the video being cast. The JWPlayerDelegate provides you the playback callbacks while casting.


TIP

The JW Player iOS Best Practice Apps repository contains several best practice apps, including an example of the code necessary for a casting experience. The targets containing relevant code are named AirPlay (Swift) and JWAirPlay (Objective-C).

Updated 7 months ago


Add an AirPlay button to an app


Learn how to add an AirPlay button with the iOS SDK

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.