2021 EssentialsAudio & Video
WWDC21 · 8 min · Essentials / Audio & Video
What’s new in AVKit
Learn about enhancements to Picture in Picture and full screen improvements on macOS. Explore the new content source API, and learn how AVPictureInPictureController supports AVSampleBufferDisplayLayer, as well as recommended steps for an app to provide a seamless full screen experience on macOS or in a Mac Catalyst app.
Watch at developer.apple.com ↗Code shown on screen · 12 snippets
New canStartPictureInPictureAutomaticallyFromInline property
// New property on AVPlayerViewController / AVPictureInPictureController.
var canStartPictureInPictureAutomaticallyFromInline: Bool { get set } Setting up AVPictureInPictureController with an AVPlayerLayer
func setupPictureInPicture() {
// Ensure PiP is supported by current device.
if AVPictureInPictureController.isPictureInPictureSupported() {
// Create a new controller, passing the reference to the AVPlayerLayer.
pictureInPictureController = AVPictureInPictureController(playerLayer: playerLayer)
pictureInPictureController.delegate = self
// Observe AVPictureInPictureController.isPictureInPicturePossible to update the PiP
// button’s enabled state.
} else {
// PiP isn't supported by the current device. Disable the PiP button.
pictureInPictureButton.isEnabled = false
}
} Starting and stopping picture in picture
@IBAction func togglePictureInPictureMode(_ sender: UIButton) {
if pictureInPictureController.isPictureInPictureActive {
pictureInPictureController.stopPictureInPicture()
} else {
pictureInPictureController.startPictureInPicture()
}
} AVPictureInPictureSampleBufferPlaybackDelegate
public protocol AVPictureInPictureSampleBufferPlaybackDelegate: NSObjectProtocol{
// Delegate is responsible for:
//
// - Supplying playback state information for PiP UI.
// - Responding to user input from PiP UI.
} Toggle playback of the video and seek back / ahead 15 seconds
func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, setPlaying playing: Bool)
func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, skipByInterval skipInterval: CMTime, completion completionHandler: @escaping () -› Void) Provide elapsed time information
func pictureInPictureControllerTimeRangeForPlayback(_ pictureInPictureController: AVPictureInPictureController) -> CMTimeRange Choose appropriate media variant for render size
func pictureInPictureController(_ pictureInPictureController: AVPictureInPictureController, didTransitionToRenderSize newRenderSize: CMVideoDimensions) Update playback state
func pictureInPictureControllerIsPlaybackPaused(pictureInPictureController: AVPictureInPictureController) -> Bool iOS / MacCatalyst - Persist full screen playback
func playerViewController(_ playerViewController: AVPlayerViewController, willBeginFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: nil) { context in
// Keep a strong reference to the playerViewController while in full screen.
self.detachedPlayerViewController = playerViewController
}
} iOS / MacCatalyst - Release the playerViewController
func playerViewController(_ playerViewController: AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator){
coordinator.animate(alongsideTransition: nil) { context in
// Stop keeping the playerViewController alive when transition completes,
self.detachedPlayerViewController = nil
}
} Persist full screen playback on macOS
func playerViewWillEnterFullScreen(_ playerView: AVPlayerView) {
// Start keeping the player view alive while it is not in the view hierarchy.
self.detachedPlayerView = playerView
}
func playerViewWillExitFullScreen(_ playerView: AVPlayerView) {
// Stop keeping the player view alive.
self.detachedPlayerView = nil
} Restoring UI when exiting full screen
// Restoring UI when exiting full screen
// iOS / MacCatalyst
func playerViewControllerRestoreUserInterfaceForFullScreenExit(_ playerViewController: AVPlayerViewController) async -> Bool {
// Custom UI restoration logic
return true
}
// macOS
func playerViewRestoreUserInterfaceForFullScreenExit(_ playerView: AVPlayerView) async -> Bool {
// custom UI restore logic here
return true
} Resources
Related sessions
-
17 min -
11 min -
40 min