Sample CodeiOS, iPadOS, Mac CatalystReviewed 2026-07-21View on Apple Developer

Playing custom audio with your own player

At a glance

Item Summary
Purpose Construct an audio player to play your custom audio data, and optionally take advantage of the advanced features of AirPlay 2.
App architecture A Swift sample with the source-visible chain SampleBufferPlayerAppContentViewRemoteCommandHandlerAVFoundation / MediaPlayer APIs.
Main patterns Protocol-oriented abstraction
Project style 9 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: await suspension point, DispatchSemaphore, DispatchQueue(label:), DispatchQueue.main.async; none alone proves a background thread.
State/event model Source-visible mechanisms: SwiftUI state property wrapper, @Observable, NotificationCenter.
Key frameworks/packages AVFoundation, MediaPlayer, SwiftUI, AVKit, CoreAudio; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── SampleBufferPlayer/
│   ├── SampleBufferPlayerApp.swift
│   ├── ContentView.swift
│   ├── Player/
│   │   ├── SampleBufferPlayer.swift
│   │   ├── SampleBufferSerializer.swift
│   │   ├── SampleBufferItem.swift
│   │   └── SampleBufferSource.swift
│   └── General/
│       ├── RemoteCommandCenter.swift
│       ├── NowPlayingCenter.swift
│       └── PlaylistItem.swift
├── Configuration/
│   └── SampleCode.xcconfig
└── SampleBufferPlayer.xcodeproj/
    ├── .xcodesamplecode.plist
    └── project.pbxproj

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 14 source declaration(s).

Overall architecture

Reference code

SampleBufferPlayer/SampleBufferPlayerApp.swift:10 — architecture anchor

@main
struct SampleBufferPlayerApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

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 AVFAudio.

Ownership and state

Ownership evidence

SampleBufferPlayer/ContentView.swift:15 — stored dependency or nearest verified ownership anchor

struct ContentView: View {
    // ...
    @State private var sampleBufferPlayer = SampleBufferPlayer()
    // ...
}
Owner Object or state Relationship Mutation authority
ContentView SampleBufferPlayer (sampleBufferPlayer) owns wrapper-managed state Owning lexical scope
Playlist Array (items) owns value state App/module collaborators
Playlist Int (currentIndex) owns value state App/module collaborators
Playlist DispatchSemaphore (atomicitySemaphore) creates and retains Initialized by the owner; the binding is immutable

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
Suspension boundary await suspension point The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. SampleBufferPlayer/ContentView.swift:111
Synchronization DispatchSemaphore The source references a synchronization primitive; the protected state requires surrounding review. SampleBufferPlayer/Player/SampleBufferPlayer.swift:32
Queue scheduling DispatchQueue(label:) The source constructs a dispatch queue; its label alone does not prove a thread. SampleBufferPlayer/Player/SampleBufferSerializer.swift:41
Queue scheduling DispatchQueue.main.async The source addresses the main dispatch queue. SampleBufferPlayer/Player/SampleBufferSerializer.swift:530

@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

SampleBufferPlayer/ContentView.swift:111 — representative execution boundary

struct ContentView: View {
    // ...
            await sampleBufferPlayer.loadPlaylist()
    // ...
}

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. SampleBufferPlayer/ContentView.swift:15
State propagation @Observable Observation macro publishes source-visible changes. SampleBufferPlayer/Player/SampleBufferPlayer.swift:10
State propagation NotificationCenter NotificationCenter distributes named process-local events. SampleBufferPlayer/Player/SampleBufferPlayer.swift:50
Source import AVFoundation The cited file imports this module; runtime use and architectural role are not inferred. SampleBufferPlayer/General/PlaylistItem.swift:8
Source import MediaPlayer The cited file imports this module; runtime use and architectural role are not inferred. SampleBufferPlayer/ContentView.swift:11
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. SampleBufferPlayer/ContentView.swift:9
Source import AVKit The cited file imports this module; runtime use and architectural role are not inferred. SampleBufferPlayer/ContentView.swift:10
Source import CoreAudio The cited file imports this module; runtime use and architectural role are not inferred. SampleBufferPlayer/Player/SampleBufferSource.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

SampleBufferPlayer/General/RemoteCommandCenter.swift:19 — representative type boundary

protocol RemoteCommandHandler: AnyObject {
    func performRemoteCommand(_: RemoteCommand)
}
Type Responsibility Depends on or conforms to
SampleBufferPlayerApp Application entry and top-level composition App
RemoteCommandHandler Defines a capability or collaboration contract AnyObject
ContentView User-interface presentation and input forwarding View
SampleBufferPlayer Owns media or timeline playback Concrete collaborators/imported frameworks
RemoteCommand Represents or runs a user/system command Concrete collaborators/imported frameworks
Representable Represents a feature value or composable behavior Concrete collaborators/imported frameworks
Playlist Represents a feature value or composable behavior Concrete collaborators/imported frameworks
RemoteCommandCenter Owns feature behavior and collaborator lifecycle Concrete collaborators/imported frameworks
SampleBufferSerializer Owns feature behavior and collaborator lifecycle Concrete collaborators/imported frameworks
LogComponentType Defines a closed set of feature states or choices Concrete collaborators/imported frameworks

The source explicitly defines local protocol relationships: SampleBufferPlayerRemoteCommandHandler.

Access control

Symbol Access Verified effect Likely rationale
sampleBufferPlayer (SampleBufferPlayer/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.
editMode (SampleBufferPlayer/ContentView.swift:18) 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.
hash (SampleBufferPlayer/General/PlaylistItem.swift:62) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
PlaylistItem (SampleBufferPlayer/General/PlaylistItem.swift:66) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.

Reference code

SampleBufferPlayer/ContentView.swift:15 — representative boundary

struct ContentView: View {
    // ...
    @State private var sampleBufferPlayer = SampleBufferPlayer()
    // ...
}

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 SampleBufferPlayerApp The source’s App suffix makes this role explicit.
Represents or runs a user/system command RemoteCommand The source’s Command suffix makes this role explicit.
Handles callbacks or feature events RemoteCommandHandler The source’s Handler suffix makes this role explicit.
Owns media or timeline playback SampleBufferPlayer The source’s Player 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
Protocol-oriented abstraction SampleBufferPlayer/Player/SampleBufferPlayer.swift:457 A local protocol and concrete conformance create an explicit capability boundary.

Naming conventions

  • Types: App: SampleBufferPlayerApp; Command: RemoteCommand; Handler: RemoteCommandHandler; Player: SampleBufferPlayer; View: ContentView.
  • Protocols: RemoteCommandHandler.
  • Methods: transportButton, makeUIView, updateUIView, loadPlaylist, restorePlaylist, loadItem, setUpAudioSession, item.
  • Files: SampleBufferPlayer/SampleBufferPlayerApp.swift, SampleBufferPlayer/ContentView.swift, SampleBufferPlayer/Player/SampleBufferPlayer.swift, SampleBufferPlayer/General/RemoteCommandCenter.swift, SampleBufferPlayer/Player/SampleBufferSerializer.swift, SampleBufferPlayer/General/NowPlayingCenter.swift.

Architecture takeaways

  • SampleBufferPlayerApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches AVFoundation, MediaPlayer, SwiftUI, 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.
  • Local protocol relationships provide an explicit substitution boundary.

Source map

Source file Relevant symbols
SampleBufferPlayer/SampleBufferPlayerApp.swift Cited implementation, SampleBufferPlayerApp
SampleBufferPlayer/ContentView.swift Cited implementation, await suspension point, SwiftUI state property wrapper, MediaPlayer, SwiftUI, AVKit, ContentView, Representable
SampleBufferPlayer/General/RemoteCommandCenter.swift RemoteCommandHandler, RemoteCommand, RemoteCommandCenter
SampleBufferPlayer/General/PlaylistItem.swift Cited implementation, AVFoundation, PlaylistItem
SampleBufferPlayer/Player/SampleBufferPlayer.swift Cited implementation, DispatchSemaphore, @Observable, NotificationCenter, SampleBufferPlayer, Playlist
SampleBufferPlayer/Player/SampleBufferSerializer.swift DispatchQueue(label:), DispatchQueue.main.async, SampleBufferSerializer, LogComponentType
SampleBufferPlayer/Player/SampleBufferSource.swift CoreAudio, SampleBufferSource
SampleBufferPlayer/General/NowPlayingCenter.swift NowPlayingCenter
SampleBufferPlayer/Player/SampleBufferItem.swift SampleBufferItem