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

Capturing Spatial Audio in your iOS app

At a glance

Item Summary
Purpose Enhance your app’s audio recording capabilities by supporting Spatial Audio capture.
App architecture A Swift sample with the source-visible chain SpatialAudioDemoAppContentViewAudioPlayerViewModelAudioRecorderAVFoundation APIs.
Main patterns Model-View-ViewModel, 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: DispatchQueue.main, Sendable or @Sendable, DispatchQueue(label:), async declaration or closure, Task; none alone proves a background thread.
State/event model Source-visible mechanisms: SwiftUI state property wrapper, @Observable, AnyCancellable, receive(on:).
Key frameworks/packages SwiftUI, AVFoundation, Combine, CoreFoundation, CoreMedia; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── SpatialAudioDemo/
│   ├── SpatialAudioDemoApp.swift
│   ├── AudioPlayerViewModel.swift
│   ├── ContentView.swift
│   ├── AudioPlayerView.swift
│   ├── AudioRecorder.swift
│   ├── AudioVisualizerView.swift
│   └── Info.plist
├── Configuration/
│   └── SampleCode.xcconfig
└── SpatialAudioDemo.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 7 source declaration(s).

Overall architecture

Reference code

SpatialAudioDemo/SpatialAudioDemoApp.swift:10 — architecture anchor

@main
struct SpatialAudioDemoApp: 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 AVFoundation.

Ownership and state

Ownership evidence

SpatialAudioDemo/AudioPlayerViewModel.swift:15 — stored dependency or nearest verified ownership anchor

@Observable class AudioPlayerViewModel {
    // ...
    var player: AVPlayer?
    // ...
}
Owner Object or state Relationship Mutation authority
AudioPlayerViewModel AVPlayer (player) stores or receives App/module collaborators
AudioPlayerViewModel Any (timeObserver) stores or receives Owning lexical scope
AudioPlayerViewModel Bool (isPlaying) owns value state App/module collaborators
AudioPlayerViewModel Double (currentTime) owns value state 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
Queue scheduling DispatchQueue.main The source addresses the main dispatch queue. SpatialAudioDemo/AudioPlayerViewModel.swift:38
Transfer contract Sendable or @Sendable The source declares a sendability boundary; this alone does not synchronize mutable state. SpatialAudioDemo/AudioRecorder.swift:15
Queue scheduling DispatchQueue(label:) The source constructs a dispatch queue; its label alone does not prove a thread. SpatialAudioDemo/AudioRecorder.swift:82
Suspension boundary async declaration or closure The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. SpatialAudioDemo/AudioRecorder.swift:259
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. SpatialAudioDemo/ContentView.swift:58

@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

SpatialAudioDemo/AudioPlayerViewModel.swift:38 — representative execution boundary

@Observable class AudioPlayerViewModel {
    // ...
           .receive(on: DispatchQueue.main)
    // ...
}

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. SpatialAudioDemo/AudioPlayerView.swift:19
State propagation @Observable Observation macro publishes source-visible changes. SpatialAudioDemo/AudioPlayerViewModel.swift:12
State propagation AnyCancellable A cancellable value records subscription lifetime management. SpatialAudioDemo/AudioPlayerViewModel.swift:29
Combine scheduling receive(on:) receive(on:) selects the scheduler for downstream delivery. SpatialAudioDemo/AudioPlayerViewModel.swift:38
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. SpatialAudioDemo/AudioPlayerView.swift:8
Source import AVFoundation The cited file imports this module; runtime use and architectural role are not inferred. SpatialAudioDemo/AudioPlayerViewModel.swift:8
Source import Combine The cited file imports this module; runtime use and architectural role are not inferred. SpatialAudioDemo/AudioPlayerViewModel.swift:9
Source import CoreFoundation The cited file imports this module; runtime use and architectural role are not inferred. SpatialAudioDemo/AudioRecorder.swift:10

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

SpatialAudioDemo/SpatialAudioDemoApp.swift:11 — representative type boundary

@main
struct SpatialAudioDemoApp: App {
    // ...
            ContentView()
    // ...
}
Type Responsibility Depends on or conforms to
SpatialAudioDemoApp Application entry and top-level composition App
AudioPlayerViewModel UI-facing state and feature coordination Concrete collaborators/imported frameworks
ContentView User-interface presentation and input forwarding View
AudioPlayerView User-interface presentation and input forwarding View
AudioRecorder Owns capture or recording work NSObject, AVCaptureAudioDataOutputSampleBufferDelegate, @unchecked Sendable
AudioVisualizerView User-interface presentation and input forwarding View
LiveWaveformShape Represents a feature value or composable behavior Shape

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
viewModel (SpatialAudioDemo/AudioPlayerView.swift:19) 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.
timeObserver (SpatialAudioDemo/AudioPlayerViewModel.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.
cancellables (SpatialAudioDemo/AudioPlayerViewModel.swift:29) 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.
addPeriodicTimeObserver (SpatialAudioDemo/AudioPlayerViewModel.swift:90) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: hide an implementation step that is not part of the collaboration surface.

Reference code

SpatialAudioDemo/AudioPlayerView.swift:19 — representative boundary

struct AudioPlayerView: View {
    @State private var viewModel = AudioPlayerViewModel()
    // ...
}

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 SpatialAudioDemoApp The source’s App suffix makes this role explicit.
Owns capture or recording work AudioRecorder The source’s Recorder suffix makes this role explicit.
User-interface presentation and input forwarding AudioPlayerView, AudioVisualizerView, ContentView The source’s View suffix makes this role explicit.
UI-facing state and feature coordination AudioPlayerViewModel The source’s ViewModel suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Model-View-ViewModel SpatialAudioDemo/AudioPlayerViewModel.swift:12 Role-named view models keep UI-facing state or coordination outside view declarations.
Delegate or data-source callbacks SpatialAudioDemo/AudioRecorder.swift:15 Callback protocols invert event delivery back into the sample’s owner.

Naming conventions

  • Types: App: SpatialAudioDemoApp; Recorder: AudioRecorder; View: AudioPlayerView, AudioVisualizerView, ContentView; ViewModel: AudioPlayerViewModel.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: loadAudio, play, pause, skipAudioFiveSeconds, rewindAudioFiveSeconds, seek, seekToAndPlay, addPeriodicTimeObserver.
  • Files: SpatialAudioDemo/SpatialAudioDemoApp.swift, SpatialAudioDemo/AudioPlayerViewModel.swift, SpatialAudioDemo/ContentView.swift, SpatialAudioDemo/AudioPlayerView.swift, SpatialAudioDemo/AudioRecorder.swift, SpatialAudioDemo/AudioVisualizerView.swift.

Architecture takeaways

  • SpatialAudioDemoApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, AVFoundation, CoreFoundation, CoreMedia 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
SpatialAudioDemo/SpatialAudioDemoApp.swift Cited implementation, SpatialAudioDemoApp
SpatialAudioDemo/AudioPlayerViewModel.swift Cited implementation, AudioPlayerViewModel, DispatchQueue.main, @Observable, AnyCancellable, receive(on:), AVFoundation, Combine
SpatialAudioDemo/AudioPlayerView.swift Cited implementation, SwiftUI state property wrapper, SwiftUI, AudioPlayerView
SpatialAudioDemo/AudioRecorder.swift Cited implementation, Sendable or @Sendable, DispatchQueue(label:), async declaration or closure, CoreFoundation, AudioRecorder
SpatialAudioDemo/ContentView.swift Task, ContentView, LiveWaveformShape
SpatialAudioDemo/AudioVisualizerView.swift AudioVisualizerView