$npx -y skills add dpearson2699/swift-ios-skills --skill avkitCreate media playback experiences using AVKit. Use when adding video players with AVPlayerViewController, enabling Picture-in-Picture, routing media with AirPlay, using SwiftUI VideoPlayer views, configuring transport controls, displaying subtitles and closed captions, or integra
| 1 | # AVKit |
| 2 | |
| 3 | High-level media playback UI built on AVFoundation. Provides system-standard |
| 4 | video players, Picture-in-Picture, AirPlay routing, transport controls, and |
| 5 | subtitle/caption display. Targets Swift 6.3 / iOS 26+. |
| 6 | |
| 7 | ## Contents |
| 8 | |
| 9 | - [Setup](#setup) |
| 10 | - [AVPlayerViewController](#avplayerviewcontroller) |
| 11 | - [SwiftUI VideoPlayer](#swiftui-videoplayer) |
| 12 | - [Picture-in-Picture](#picture-in-picture) |
| 13 | - [AirPlay](#airplay) |
| 14 | - [Transport Controls and Playback Speed](#transport-controls-and-playback-speed) |
| 15 | - [Subtitles and Closed Captions](#subtitles-and-closed-captions) |
| 16 | - [Common Mistakes](#common-mistakes) |
| 17 | - [Review Checklist](#review-checklist) |
| 18 | - [References](#references) |
| 19 | |
| 20 | ## Setup |
| 21 | |
| 22 | ### Audio Session Configuration |
| 23 | |
| 24 | Playback apps need an audio session category and the matching background mode |
| 25 | when they support background audio, AirPlay, or PiP. |
| 26 | |
| 27 | 1. Enable Background Modes > Audio, AirPlay, and Picture in Picture (the |
| 28 | `audio` value in `UIBackgroundModes`) |
| 29 | 2. Set the audio session category to `.playback` |
| 30 | 3. Defer `setActive(true)` until playback begins so you do not interrupt other |
| 31 | audio prematurely |
| 32 | |
| 33 | ```swift |
| 34 | import AVFoundation |
| 35 | |
| 36 | func configureAudioSessionForPlayback() { |
| 37 | let session = AVAudioSession.sharedInstance() |
| 38 | do { |
| 39 | try session.setCategory(.playback, mode: .moviePlayback) |
| 40 | } catch { |
| 41 | print("Audio session category failed: \(error)") |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func activateAudioSessionWhenPlaybackBegins() { |
| 46 | do { |
| 47 | try AVAudioSession.sharedInstance().setActive(true) |
| 48 | } catch { |
| 49 | print("Audio session activation failed: \(error)") |
| 50 | } |
| 51 | } |
| 52 | ``` |
| 53 | |
| 54 | ### Imports |
| 55 | |
| 56 | ```swift |
| 57 | import AVKit // AVPlayerViewController, VideoPlayer, PiP |
| 58 | import AVFoundation // AVPlayer, AVPlayerItem, AVAsset |
| 59 | ``` |
| 60 | |
| 61 | ## AVPlayerViewController |
| 62 | |
| 63 | `AVPlayerViewController` is the standard UIKit player. It provides system |
| 64 | playback controls, PiP, AirPlay, subtitles, and frame analysis out of the box. |
| 65 | Do not subclass it. |
| 66 | |
| 67 | ### Basic Presentation (Full Screen) |
| 68 | |
| 69 | ```swift |
| 70 | import AVKit |
| 71 | |
| 72 | func presentPlayer(from viewController: UIViewController, url: URL) { |
| 73 | let player = AVPlayer(url: url) |
| 74 | let playerVC = AVPlayerViewController() |
| 75 | playerVC.player = player |
| 76 | |
| 77 | viewController.present(playerVC, animated: true) { |
| 78 | player.play() |
| 79 | } |
| 80 | } |
| 81 | ``` |
| 82 | |
| 83 | ### Inline (Embedded) Playback |
| 84 | |
| 85 | Add `AVPlayerViewController` as a child view controller for inline playback. |
| 86 | Call `addChild`, add the view with constraints, then call `didMove(toParent:)`. |
| 87 | |
| 88 | ```swift |
| 89 | func embedPlayer(in parent: UIViewController, container: UIView, url: URL) { |
| 90 | let playerVC = AVPlayerViewController() |
| 91 | playerVC.player = AVPlayer(url: url) |
| 92 | |
| 93 | parent.addChild(playerVC) |
| 94 | container.addSubview(playerVC.view) |
| 95 | playerVC.view.translatesAutoresizingMaskIntoConstraints = false |
| 96 | NSLayoutConstraint.activate([ |
| 97 | playerVC.view.leadingAnchor.constraint(equalTo: container.leadingAnchor), |
| 98 | playerVC.view.trailingAnchor.constraint(equalTo: container.trailingAnchor), |
| 99 | playerVC.view.topAnchor.constraint(equalTo: container.topAnchor), |
| 100 | playerVC.view.bottomAnchor.constraint(equalTo: container.bottomAnchor) |
| 101 | ]) |
| 102 | playerVC.didMove(toParent: parent) |
| 103 | } |
| 104 | ``` |
| 105 | |
| 106 | ### Key Properties |
| 107 | |
| 108 | ```swift |
| 109 | playerVC.showsPlaybackControls = true // Show/hide system controls |
| 110 | playerVC.videoGravity = .resizeAspect // .resizeAspectFill to crop |
| 111 | playerVC.entersFullScreenWhenPlaybackBegins = false |
| 112 | playerVC.exitsFullScreenWhenPlaybackEnds = true |
| 113 | playerVC.updatesNowPlayingInfoCenter = true // Auto-updates MPNowPlayingInfoCenter |
| 114 | ``` |
| 115 | |
| 116 | Use `contentOverlayView` to add non-interactive views (watermarks, logos) |
| 117 | between the video and transport controls. |
| 118 | |
| 119 | ### Delegate |
| 120 | |
| 121 | Adopt `AVPlayerViewControllerDelegate` to respond to full-screen transitions, |
| 122 | PiP lifecycle events, interstitial playback, and media selection changes. |
| 123 | Use the transition coordinator's `animate(alongsideTransition:completion:)` to |
| 124 | synchronize your UI with full-screen animations. |
| 125 | |
| 126 | ### Display Readiness |
| 127 | |
| 128 | Observe `isReadyForDisplay` before showing the player to avoid a black flash: |
| 129 | |
| 130 | ```swift |
| 131 | let observation = playerVC.observe(\.isReadyForDisplay) { observed, _ in |
| 132 | if observed.isReadyForDisplay { |
| 133 | // Safe to show the player view |
| 134 | } |
| 135 | } |
| 136 | ``` |
| 137 | |
| 138 | ## SwiftUI VideoPlayer |
| 139 | |
| 140 | The `VideoPlayer` SwiftUI view wraps AVKit's playback UI. |
| 141 | |
| 142 | ### Basic Usage |
| 143 | |
| 144 | ```swift |
| 145 | import SwiftUI |
| 146 | import AVKit |
| 147 | |
| 148 | struct PlayerView: View { |
| 149 | @State private var player: AVPlayer? |
| 150 | |
| 151 | var body: some View { |
| 152 | Group { |
| 153 | if let player { |
| 154 | VideoPlayer(player: player) |
| 155 | .frame(height: 300) |
| 156 | } else { |