Class

AudioPlaybackController

A controller that manages audio playback of a resource.

Declaration

class AudioPlaybackController

Overview

You receive an audio playback controller by calling an entity’s prepareAudio(_:) method. You typically pass an AudioFileResource instance to this call that tells the playback controller how to stream the contents of an audio file from disk, or from a URL.

During playback, the audio appears to come from the entity that you used to create the controller. As the user moves around the AR scene, RealityKit modulates the characteristics of the audio to account for the user’s location.

After playback completes, or if you call the stop() method, the audio controller resets so that you can play the resource from the beginning. Alternatively, you can configure the audio to loop indefinitely by setting the loops property of the audio resource to true.

Look for one of the events in AudioEvents if you want to be alerted when certain aspects of audio playback occur.

Topics

Identifying the Controller

var id: ObjectIdentifier

The stable identity of the entity associated with self.

var id: UInt64

The stable identity of the controller.

typealias AudioPlaybackController.ID

A type of unique identifier used for a playback controller.

Managing the Resource

let resource: AudioResource

The resource that provides the audio stream.

Setting the Volume

var gain: AudioPlaybackController.Decibel

The gain in decibels of the audio playback controller output.

func fade(to: AudioPlaybackController.Decibel, duration: TimeInterval)

Transitions the gain to the given value over a time interval using a linear curve.

typealias AudioPlaybackController.Decibel

A type for expressing gain in decibels.

Setting the Speed

var speed: Double

The rate of playback of the audio resource, with a range from half speed to double speed.

Setting the Reverb

var reverbSendLevel: AudioPlaybackController.Decibel

The send level in decibels from the playback controller to the reverb system.

Starting and Stopping Audio Playback

func play()

Plays the audio resource.

func pause()

Pauses playback of the audio resource while maintaining the position in the audio stream.

func stop()

Stops playback of the audio resource and discards the location in the audio stream.

var isPlaying: Bool

A Boolean that indicates whether playback is currently active.

Handling Completion

var completionHandler: (() -> Void)?

A closure that the playback controller executes when it comes to the end of the audio stream.

Finding the Associated Entity

var entity: Entity?

The entity from which the audio stream emanates.

Relationships

Conforms To

See Also

Audio Playback

class AudioResource

An audio resource that can be played by an entity.

class AudioFileResource

An audio resource that you load from a file or from a URL.

enum AudioEvents

Events associated with audio playback.