<!--
{
  "documentType" : "article",
  "framework" : "AudioToolbox",
  "identifier" : "/documentation/AudioToolbox/spatializing-sound-from-a-uiscene",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Anchoring sound to a window or volume"
}
-->

# Anchoring sound to a window or volume

Provide unique app experiences by attaching sounds to windows and volumes in 3D space.

## Overview

Many audio playback APIs have a property to configure their 3D spatial rendering using the [`SpatialAudioExperience`](/documentation/AudioToolbox/SpatialAudioExperience) type [`HeadTrackedSpatialAudio`](/documentation/AudioToolbox/HeadTrackedSpatialAudio).
This article shows how to take advantage of [`HeadTrackedSpatialAudio`](/documentation/AudioToolbox/HeadTrackedSpatialAudio) to place each sound at the center of its intended <doc://com.apple.documentation/documentation/UIKit/UIScene> in your multiwindow or multivolume application.

![An illustration of sound spatializing from two windows.](images/com.apple.audiotoolbox/spatializing-sound-from-a-uiscene-01@2x.png)

## Get the scene’s identifier

Placing a sound on a specific <doc://com.apple.documentation/documentation/UIKit/UIScene> requires knowledge of the target scene’s <doc://com.apple.documentation/documentation/UIKit/UISceneSession/persistentIdentifier>.
In a SwiftUI application, that means adding both a <doc://com.apple.documentation/documentation/UIKit/UIApplicationDelegate> and <doc://com.apple.documentation/documentation/UIKit/UISceneDelegate> to your SwiftUI App:

```swift
import SwiftUI 

@main
struct MyApplication: App {
    @UIApplicationDelegateAdaptor var delegate: MyAppDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

class MyAppDelegate: NSObject, UIApplicationDelegate, ObservableObject {
    func application(_ application: UIApplication,
                     configurationForConnecting connectingSceneSession: UISceneSession,
                     options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        let sceneConfig = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
        sceneConfig.delegateClass = MySceneDelegate.self
        return sceneConfig
    }
}

class MySceneDelegate: NSObject, UISceneDelegate, ObservableObject {
    var sceneIdentifier: String?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        sceneIdentifier = session.persistentIdentifier
    }
}
```

The following code makes the identifier for each <doc://com.apple.documentation/documentation/UIKit/UIScene> accessible from any SwiftUI <doc://com.apple.documentation/documentation/SwiftUI/View> using your <doc://com.apple.documentation/documentation/UIKit/UISceneDelegate> as an <doc://com.apple.documentation/documentation/SwiftUI/EnvironmentObject>:

```swift
import SwiftUI

struct ContentView: View {
    @EnvironmentObject var sceneDelegate: MySceneDelegate

    var body: some View {
        Text("\(String(describing: sceneDelegate.sceneIdentifier))")
    }
}
```

## Anchor the sound to the scene

With a <doc://com.apple.documentation/documentation/UIKit/UIScene> identifier in-hand, configure each sound using a [`HeadTrackedSpatialAudio`](/documentation/AudioToolbox/HeadTrackedSpatialAudio) structure.

```swift
import SwiftUI
import AVFAudio

struct ContentView: View {
    @EnvironmentObject var sceneDelegate: MySceneDelegate
    
    @State var player: AVAudioPlayer? = {
        guard let url = Bundle.main.url(forResource: "my_sound", withExtension: "wav") else {
            return nil
        }
        return try? AVAudioPlayer(contentsOf: url)
    }()

    var body: some View {
        Text("Hello, Sound!")
    }
    .onAppear {
        guard let player = self.player, let sceneID = self.sceneDelegate.sceneIdentifier else {
            return
        }
        
        player.intendedSpatialExperience = .headTracked(.scene(identifier: sceneID))
        player.play()
    }
}
```

Besides just <doc://com.apple.documentation/documentation/AVFAudio/AVAudioPlayer>, you can also use [`SpatialAudioExperience`](/documentation/AudioToolbox/SpatialAudioExperience) types with the other playback APIs listed below.

## Spatialize system and alert sounds

Configure the spatial audio experience of your system and alert sounds using:

- [`AudioServicesPlaySystemSound(_:spatialExperience:)`](/documentation/AudioToolbox/AudioServicesPlaySystemSound(_:spatialExperience:))
- [`AudioServicesPlayAlertSound(_:spatialExperience:)`](/documentation/AudioToolbox/AudioServicesPlayAlertSound(_:spatialExperience:))

## Spatialize audio-only playback APIs

Configure the spatial audio experience of audio-only playback APIs using the <doc://com.apple.documentation/documentation/AVFAudio/AVAudioPlayer/intendedSpatialExperience-27klj> property on:

- [AVAudioPlayer](doc://com.apple.documentation/documentation/AVFAudio/AVAudioPlayer/intendedSpatialExperience-27klj)
- [AVAudioOutputNode](doc://com.apple.documentation/documentation/AVFAudio/AVAudioOutputNode/intendedSpatialExperience-3ts59)
- [AUAudioUnit](doc://com.apple.documentation/documentation/AudioToolbox/AUAudioUnit/intendedSpatialExperience-7uqrm)
- [CHHapticEngine](doc://com.apple.documentation/documentation/CoreHaptics/CHHapticEngine/intendedSpatialExperience-55ca0)

## Spatialize audio playback APIs that also have video

Setting a scene identifier on playback APIs that have video content isn’t always necessary as their sound automatically anchors to its visual counterpart. However, if there is no video or if you prefer something besides the automatic behavior, configure the spatial audio experience of these playback APIs using the <doc://com.apple.documentation/documentation/AVFoundation/AVPlayer/intendedSpatialAudioExperience-1bd87> property on:

- [AVPlayer](doc://com.apple.documentation/documentation/AVFoundation/AVPlayer/intendedSpatialAudioExperience-1bd87)
- [AVSampleBufferRenderSynchronizer](doc://com.apple.documentation/documentation/AVFoundation/AVSampleBufferRenderSynchronizer/intendedSpatialAudioExperience-3z7d3)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)