Add picture-in-picture controls to your custom player using a picture-in-picture controller.
Add picture-in-picture playback to your custom player by using the AVKit framework’s
AVPicture class. This class lets you implement the same picture-in-picture behavior found in
AVPlayer, but requires some additional work from you.
Add a UI to Your Custom Player
Begin by adding a UI to your custom player interface to enable users to begin picture-in-picture playback. Make this UI consistent with the system default UI presented by
AVPlayer. Access the standard images for controlling picture-in-picture playback by using the
picture class methods of
AVPicture. These methods return system default images to present in your UI.
Create the Picture-in-Picture Controller
You create an instance of
AVPicture to control picture-in-picture playback in your app. Before attempting to create the
AVPicture instance, verify that picture-in-picture is supported by the current hardware using the
This example creates a new instance of
AVPicture, passing it a reference to the
AVPlayer used to present the video content. You need to maintain a strong reference to the controller object for picture-in-picture functionality to work.
To participate in picture-in-picture life-cycle events, your code should adopt the
AVPicture protocol and set itself as the controller’s delegate. Also, use key-value observing (KVO) on the controller’s
is property. This property indicates whether using picture-in-picture mode is possible in the current context; for example, when an active FaceTime window is displayed. Observing this property enables you to determine when it’s appropriate to change the enabled state of your picture-in-picture button.
Handle User-Initiated Requests
AVPicture setup complete, add an @IBAction method to handle user-initiated requests to start or stop picture-in-picture playback.
Restore Control to Your App
A user taps the button in the picture-in-picture window to return control to your app. By default, this action terminates playback when control returns to the app. AVKit can’t make assumptions about how your app is structured and is unaware of how to properly restore your video playback interface. Instead, it delegates that responsibility to you.
To handle the restore process, implement the
picture delegate method and restore your player interface as needed. When the restoration is complete, call the completion handler with a value of
Dismiss Playback Controls
While picture-in-picture mode is active, dismiss playback controls in your main player and present artwork inside its bounds to indicate that picture-in-picture mode is under way. To implement this functionality, use the
picture delegate methods and take the required actions.