Sample Code

Creating an Immersive AR Experience with Audio

Use sound effects and environmental sound layers to create an engaging AR experience.

Download

Overview

This sample app uses SceneKit’s node-based audio API to associate environmental sounds with a virtual object that’s placed in the real world. Because audio is 3D positional in SceneKit by default, volume is automatically mixed based on the user’s distance from a node.

Run an AR Session and Place Virtual Content

Before you can use audio, you need to set up a session and place the object from which to play sound. For simplicity, this sample runs a world tracking configuration and places a virtual object on the first horizontal plane that it detects. For more detail about this kind of session setup, see Building Your First AR Experience. The object placement approach in this sample is similar to the one demonstrated in Handling 3D Interaction and UI Controls in Augmented Reality.

Add 3D Audio to the Scene

To play audio from a given position in 3D space, create an SCNAudioSource from an audio file. This sample loads the file from the bundle in viewDidLoad:

// Instantiate the audio source
audioSource = SCNAudioSource(fileNamed: "fireplace.mp3")!

Then, the audio source is configured and prepared:

// As an environmental sound layer, audio should play indefinitely
audioSource.loops = true
// Decode the audio from disk ahead of time to prevent a delay in playback
audioSource.load()

When you’re ready to play the sound, create an SCNAudioPlayer, passing it the audio source:

// Create a player from the source and add it to `objectNode`
objectNode.addAudioPlayer(SCNAudioPlayer(source: audioSource))

See Also

Advanced Topics

Using Vision in Real Time with ARKit

Manage Vision resources for efficient execution of a Core ML image classifier, and use SpriteKit to display image classifier output in AR.