Sample CodevisionOSReviewed 2026-07-21View on Apple Developer

Playing immersive media with AVKit

At a glance

Item Summary
Purpose Adopt the system playback interface to provide an immersive video watching experience.
App architecture A Swift sample with the source-visible chain AVKitImmersivePlaybackAppContentViewPlayerModelSceneProviderAVKit APIs.
Main patterns Delegate or data-source callbacks
Project style 6 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: @MainActor, Task, await suspension point; none alone proves a background thread.
State/event model Source-visible mechanisms: SwiftUI state property wrapper, @Observable.
Key frameworks/packages SwiftUI, UIKit, AVKit, Foundation, Observation; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── AVKitImmersivePlayback/
│   ├── AVKitImmersivePlaybackApp.swift
│   ├── SceneProvider.swift
│   ├── ContentView.swift
│   ├── PlayerModel.swift
│   ├── Video.swift
│   ├── VideoCard.swift
│   └── Info.plist
├── AVKitImmersivePlayback.xcodeproj/
│   ├── .xcodesamplecode.plist
│   └── project.pbxproj
└── Configuration/
    └── SampleCode.xcconfig

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Swift.
  • The verified tree contains 4 project/configuration file(s) and 7 source declaration(s).

Overall architecture

Reference code

AVKitImmersivePlayback/AVKitImmersivePlaybackApp.swift:10 — architecture anchor

@main
struct AVKitImmersivePlaybackApp: App {

    @State private var player = PlayerModel()
    @UIApplicationDelegateAdaptor private var appDelegate: AppDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(player)
        }
    }
}

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into AVKit.

Ownership and state

Ownership evidence

AVKitImmersivePlayback/AVKitImmersivePlaybackApp.swift:13 — stored dependency or nearest verified ownership anchor

@main
struct AVKitImmersivePlaybackApp: App {
    // ...
    @State private var player = PlayerModel()
    // ...
}
Owner Object or state Relationship Mutation authority
AVKitImmersivePlaybackApp PlayerModel (player) owns wrapper-managed state Owning lexical scope
AVKitImmersivePlaybackApp AppDelegate (appDelegate) stores or receives Owning lexical scope
SceneProvider UIScene (scene) stores or receives App/module collaborators
PlayerModel UIScene (scene) stores or receives App/module collaborators

Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.

Concurrency, scheduling, and thread safety

Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.

Concern Source mechanism Verified placement or handoff Evidence
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. AVKitImmersivePlayback/PlayerModel.swift:11
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. AVKitImmersivePlayback/PlayerModel.swift:36
Suspension boundary await suspension point The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. AVKitImmersivePlayback/PlayerModel.swift:36

@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.

Reference code

AVKitImmersivePlayback/PlayerModel.swift:11 — representative execution boundary

@MainActor
@Observable
class PlayerModel {
    // ...
    var scene: UIScene?
    // ...
}

State propagation, frameworks, and dependencies

Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.

Category Mechanism or module Verified role Evidence
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. AVKitImmersivePlayback/AVKitImmersivePlaybackApp.swift:13
State propagation @Observable Observation macro publishes source-visible changes. AVKitImmersivePlayback/PlayerModel.swift:12
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. AVKitImmersivePlayback/AVKitImmersivePlaybackApp.swift:8
Source import UIKit The cited file imports this module; runtime use and architectural role are not inferred. AVKitImmersivePlayback/SceneProvider.swift:8
Source import AVKit The cited file imports this module; runtime use and architectural role are not inferred. AVKitImmersivePlayback/PlayerModel.swift:8
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. AVKitImmersivePlayback/Video.swift:8
Source import Observation The cited file imports this module; runtime use and architectural role are not inferred. AVKitImmersivePlayback/PlayerModel.swift:9

receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.

Class and protocol design

AVKitImmersivePlayback/AVKitImmersivePlaybackApp.swift:11 — representative type boundary

@main
struct AVKitImmersivePlaybackApp: App {
    // ...
    @State private var player = PlayerModel()
    // ...
}
Type Responsibility Depends on or conforms to
AVKitImmersivePlaybackApp Application entry and top-level composition App
SceneProvider Supplies a capability or framework resource NSObject, UIWindowSceneDelegate
AppDelegate Receives callback-driven events NSObject, UIApplicationDelegate
ContentView User-interface presentation and input forwarding View
PlayerModel Feature data or observable state Concrete collaborators/imported frameworks
Video Represents a feature value or composable behavior Identifiable
VideoCard Represents a feature value or composable behavior View

No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.

Access control

Symbol Access Verified effect Likely rationale
player (AVKitImmersivePlayback/AVKitImmersivePlaybackApp.swift:13) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
appDelegate (AVKitImmersivePlayback/AVKitImmersivePlaybackApp.swift:14) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
model (AVKitImmersivePlayback/ContentView.swift:14) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
sceneProvider (AVKitImmersivePlayback/ContentView.swift:15) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.

Reference code

AVKitImmersivePlayback/AVKitImmersivePlaybackApp.swift:13 — representative boundary

@main
struct AVKitImmersivePlaybackApp: App {
    // ...
    @State private var player = PlayerModel()
    // ...
}

Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.

Logic ownership and placement

Logic Owning type or file Placement rationale
Application entry and top-level composition AVKitImmersivePlaybackApp The source’s App suffix makes this role explicit.
Receives callback-driven events AppDelegate The source’s Delegate suffix makes this role explicit.
Feature data or observable state PlayerModel The source’s Model suffix makes this role explicit.
Supplies a capability or framework resource SceneProvider The source’s Provider suffix makes this role explicit.
User-interface presentation and input forwarding ContentView The source’s View suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Delegate or data-source callbacks AVKitImmersivePlayback/SceneProvider.swift:11 Callback protocols invert event delivery back into the sample’s owner.

Main application flow

Reference code

AVKitImmersivePlayback/PlayerModel.swift:67presentPlayer

    private func presentPlayer() async {
        /// Attempt transition from an `.embedded` to an `.expanded` experience.
        switch await playerViewController?.experienceController.transition(to: .expanded) {
        case .completed:
            // Begin playback if the transition completes successfully.
            player.play()
        case .reversed(reason: let reason):
            print("Unable to start playback: \(reason).")
        default:
            fatalError()
        }
    }

Naming conventions

  • Types: App: AVKitImmersivePlaybackApp; Delegate: AppDelegate; Model: PlayerModel; Provider: SceneProvider; View: ContentView.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: sceneWillEnterForeground, sceneDidBecomeActive, sceneDidDisconnect, application, playVideo, loadVideo, configurePlaybackUI, presentPlayer.
  • Files: AVKitImmersivePlayback/AVKitImmersivePlaybackApp.swift, AVKitImmersivePlayback/SceneProvider.swift, AVKitImmersivePlayback/ContentView.swift, AVKitImmersivePlayback/PlayerModel.swift, AVKitImmersivePlayback/Video.swift, AVKitImmersivePlayback/VideoCard.swift.

Architecture takeaways

  • AVKitImmersivePlaybackApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, UIKit, AVKit through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
  • Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
  • Access-control conclusions separate verified language visibility from the likely design rationale.
  • The source does not justify labeling the design protocol-oriented.

Source map

Source file Relevant symbols
AVKitImmersivePlayback/AVKitImmersivePlaybackApp.swift Cited implementation, AVKitImmersivePlaybackApp, SwiftUI state property wrapper, SwiftUI
AVKitImmersivePlayback/ContentView.swift Cited implementation, ContentView
AVKitImmersivePlayback/SceneProvider.swift Cited implementation, UIKit, SceneProvider, AppDelegate
AVKitImmersivePlayback/PlayerModel.swift @MainActor, Task, await suspension point, @Observable, AVKit, Observation, PlayerModel
AVKitImmersivePlayback/Video.swift Foundation, Video
AVKitImmersivePlayback/VideoCard.swift VideoCard