Add controls to your custom player user interface to invoke Picture in Picture (PiP) playback.
Add PiP playback to your custom player by using the AVKit framework’s
AVPicture class. This class lets you implement the same PiP behavior found in
AVPlayer in your custom player.
Update Your Custom Player User Interface
Begin by adding a UI to your custom player interface to enable users to begin PiP playback. Make this UI consistent with the system default UI presented by
AVPlayer. Access the standard images for controlling PiP playback by using the
picture class properties of
AVPicture. These methods return system default images to present in your UI.
Create the Picture in Picture Controller
Create an instance of
AVPicture to control PiP playback in your app. Before attempting to create the controller instance, verify that the current hardware supports PiP playback by calling the
This example creates a new
AVPicture instance, passing it a reference to the
AVPlayer that presents the video content. You need to maintain a strong reference to the controller object for PiP functionality to work.
To participate in PiP 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
picture property. This property indicates whether using PiP mode is possible in the current context; for example, when the system is displaying an active FaceTime window. By observing this property, you can determine when it’s appropriate to change the enabled state of your PiP 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 PiP 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 you’ve structured your app 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 is active, dismiss playback controls in your main player and present artwork inside its bounds to indicate that PiP mode is active. To implement this functionality, use the
picture delegate methods and take the required actions.