Picture in Picture Quick Start
Follow the guidance in this chapter to support Picture in Picture (PiP) on eligible iPad models.
To make your video playback eligible for PiP, ensure that your Xcode project and app are configured as follows:
Set the Base SDK to “Latest iOS,” as described in Setting the Base SDK in App Distribution Guide.
In the Capabilities view for your project’s target, in the Background Modes section, select “Audio, AirPlay and Picture in Picture”.
Ensure your app’s audio session employs an appropriate category, such as
Next, employ an appropriate AVKit, AV Foundation, or WebKit class for your video playback. The choice depends on the nature of your app and the user experience you want to provide.
The AVKit framework provides the
AVPlayerViewControllerclass, which automatically displays a PiP button for your users.
If you support PiP using AVKit but want to opt out of PiP for a particular video, assign a value of
falseto the player view controller’s
AVKit also provides the
AVPictureInPictureControllerclass, which you can use with the
AVPlayerLayerclass from AV Foundation. Use this approach if you’d like to provide your own view controller and custom user interface for video playback.
If you support PiP this way but want to opt out of PiP for a particular video, do not associate that video’s
AVPictureInPictureControllerobject. As soon as you instantiate a Picture in Picture controller with a player layer, you make the player layer’s video eligible for PiP; the way to opt out is to not perform that instantiation.
The WebKit framework provides the
WKWebViewclass, which supports Picture in Picture in iOS 9.
If you support PiP using WebKit but want to opt out of PiP for a particular video, set the
allowsPictureInPictureMediaPlaybackproperty of the associated web view instance to
Never programmatically invoke Picture in Picture except in response to a user’s request.
iOS 9 automatically moves a playing full-screen video to PiP playback if a user presses the Home button or taps a notification that brings them into another app. The requirements for your video playback to be eligible for this feature are as follows:
Make your app eligible for Picture in Picture, as described earlier in this section.
Configure your video for fullscreen playback by specifying that your video’s view fills the entire bounds of your app’s window.
Manage your app’s resources carefully. When a video played by your app moves to Picture in Picture playback, the system presents the video content on your behalf while your app continues to run, potentially in the background. When your app is running in the background, discard unneeded resources such as view controllers, views, images, and data caches that your app doesn’t use when offscreen. Continue to perform appropriate, needed operations—such as video compositing, audio processing, and download of next-playing content—but take care to consume as few resources as possible. If your app consumes too many resources when running in the background for PiP, the system terminates it.