---
title: AudioGeneratorController
framework: realitykit
role: symbol
role_heading: Class
path: realitykit/audiogeneratorcontroller
---

# AudioGeneratorController

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

## Declaration

```swift
@MainActor class AudioGeneratorController
```

## Overview

Overview To receive an audio generator controller, call an entity’s prepareAudio(configuration:_:) or playAudio(configuration:_:) method. The following examples show how you can use the controller: /// 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; }; // 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() to halt the audio, and play() to restart the stream.

## Topics

### Instance Properties

- [configuration](realitykit/audiogeneratorcontroller/configuration.md)
- [entity](realitykit/audiogeneratorcontroller/entity.md)
- [gain](realitykit/audiogeneratorcontroller/gain.md)
- [isPlaying](realitykit/audiogeneratorcontroller/isplaying.md)

### Instance Methods

- [play()](realitykit/audiogeneratorcontroller/play().md)
- [stop()](realitykit/audiogeneratorcontroller/stop().md)

## Relationships

### Conforms To

- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Playback controllers

- [AudioPlaybackController](realitykit/audioplaybackcontroller.md)
- [AudioGeneratorConfiguration](realitykit/audiogeneratorconfiguration.md)
- [AudioEvents](realitykit/audioevents.md)
- [PlayAudioAction](realitykit/playaudioaction.md)
