<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 26.0.0 -",
    "visionOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "RealityKit",
  "identifier" : "/documentation/RealityKit/AudioGeneratorController",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "RealityKit"
    ],
    "preciseIdentifier" : "s:17RealityFoundation24AudioGeneratorControllerC"
  },
  "title" : "AudioGeneratorController"
}
-->

# AudioGeneratorController

A controller that manages the playback of a real-time audio stream.

```
@MainActor class AudioGeneratorController
```

## Overview

To receive an audio generator controller, call an entity’s
[`prepareAudio(configuration:_:)`](/documentation/RealityKit/Entity/prepareAudio(configuration:_:)) or [`playAudio(configuration:_:)`](/documentation/RealityKit/Entity/playAudio(configuration:_:)) method.

The following examples show how you can use the controller:

```objc
/// myHandler.mm
AVAudioSourceNodeRenderBlock myHandler = ^OSStatus(BOOL *isSilence,
                                                   const AudioTimeStamp *timestamp,
                                                   AVAudioFrameCount frameCount,
                                                   AudioBufferList *outputData) {

   double phase = FREQUENCY * timestamp->mSampleTime * (1.f / SAMPLE_RATE);
   for (int idx = 0; idx < frameCount; idx++) {
       ((Float32 *)outputData->mBuffers[0].mData)[idx] = sin(phase * 2.f * M_PI) * 0.5;
       phase += (FREQUENCY / SAMPLE_RATE);
   }

   return 0;
};
```

```swift
// Create a configuration.
var config = AudioGeneratorConfiguration(kAudioChannelLayoutTag_Mono)

// Prepare a closure that you define in myHandler.mm.
var controller = myEntity.prepareAudio(configuration: config, myHandler)

controller.gain = -3.0
controller.play()
```

During playback, the audio appears to come from the entity that you use to
create the controller. As a person moves around the MR scene, RealityKit
modulates the characteristics of the audio to account for their location.

> Note: Audio stops rendering when the system deallocates `AudioGeneratorController`.
> Create another `AudioGeneratorController` to restart audio.

Call [`stop()`](/documentation/RealityKit/AudioGeneratorController/stop()) to halt the audio, and
[`play()`](/documentation/RealityKit/AudioGeneratorController/play()) to restart the stream.

## Topics

### Instance Properties

[`let configuration: AudioGeneratorConfiguration`](/documentation/RealityKit/AudioGeneratorController/configuration)

The configuration with rendering parameters for the render handler.

[`var entity: Entity?`](/documentation/RealityKit/AudioGeneratorController/entity)

The entity the audio stream emanates from.

[`var gain: Audio.Decibel`](/documentation/RealityKit/AudioGeneratorController/gain)

The gain in decibels of the audio generator controller output.

[`var isPlaying: Bool`](/documentation/RealityKit/AudioGeneratorController/isPlaying)

A Boolean value that indicates whether playback is currently active.

### Instance Methods

[`func play()`](/documentation/RealityKit/AudioGeneratorController/play())

Begins the audio stream from the generator render handler.

[`func stop()`](/documentation/RealityKit/AudioGeneratorController/stop())

Stops playback of the render handler.

## Relationships

### Conforms To

[`Sendable`](/documentation/Swift/Sendable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

---

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)