# AudioRenderer

> Records the main audio output of the application.

## Definition

* **Type:** Class
* **Namespace:** [UnityEngine](/engine/7000.0/script-reference/unityengine.md)
* **Assembly:** UnityEngine.AudioModule

```csharp
public class AudioRenderer
```

## Remarks

Use this class for offline capture, such as rendering a cutscene or a trailer to a video file. Recording mutes the live output, so it's not suitable for capturing audio during gameplay.

While recording, Unity moves audio rendering from the audio thread onto the main thread, which can increase frame times.

Call [AudioRenderer.Start](/engine/7000.0/script-reference/unityengine/audiorenderer/start.md) to enter recording mode. While recording, Unity outputs silence to the audio device and delivers the mixed samples through [AudioRenderer.Render](/engine/7000.0/script-reference/unityengine/audiorenderer/render.md) instead. Call [AudioRenderer.Stop](/engine/7000.0/script-reference/unityengine/audiorenderer/stop.md) to leave recording mode and make audio audible again.

Call [AudioRenderer.Render](/engine/7000.0/script-reference/unityengine/audiorenderer/render.md) once per frame while recording. Use [AudioRenderer.GetSampleCountForCaptureFrame](/engine/7000.0/script-reference/unityengine/audiorenderer/getsamplecountforcaptureframe.md) to get the number of accumulated sample frames since the previous call. The buffer contains interleaved samples, so its length must equal that count multiplied by the speaker mode's channel count.

**Web Platform:** This class isn't supported in Web player builds. The API works as normal in the Editor, including when the active build target is the Web Platform.

Additional Resources: [AudioSettings](/engine/7000.0/script-reference/unityengine/audiosettings.md), [AudioSpeakerMode](/engine/7000.0/script-reference/unityengine/audiospeakermode.md)

## Examples

```csharp
using Unity.Collections;
using UnityEngine;

public class AudioCaptureExample : MonoBehaviour
{
    int m_ChannelCount;

    void OnEnable()
    {
        AudioConfiguration config = AudioSettings.GetConfiguration();
        AudioSpeakerMode speakerMode = config.speakerMode;
        m_ChannelCount = speakerMode.ChannelCount();

        AudioRenderer.Start();
    }

    void Update()
    {
        int sampleFrames = AudioRenderer.GetSampleCountForCaptureFrame();
        int length = sampleFrames * m_ChannelCount;

        NativeArray<float> buffer = new NativeArray<float>(
            length, Allocator.Temp);

        // Render runs on every frame, including frames that report no
        // samples, because it advances the count for the next frame.
        AudioRenderer.Render(buffer);

        // Write the samples to a file or an encoder here.

        buffer.Dispose();
    }

    void OnDisable()
    {
        AudioRenderer.Stop();
    }
}
```

## Static Methods

| Method                                                                                                                      | Description                                                                                                                                                                                                             |
| --------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [GetSampleCountForCaptureFrame](/engine/7000.0/script-reference/unityengine/audiorenderer/getsamplecountforcaptureframe.md) | Returns the number of sample frames available since the last time [AudioRenderer.Render](/engine/7000.0/script-reference/unityengine/audiorenderer/render.md) was called. This count depends on the frame capture rate. |
| [Render](/engine/7000.0/script-reference/unityengine/audiorenderer/render.md)                                               | Writes the audio rendered since the previous call into the supplied buffer.                                                                                                                                             |
| [Start](/engine/7000.0/script-reference/unityengine/audiorenderer/start.md)                                                 | Enters audio recording mode. After this Unity will output silence until [AudioRenderer.Stop](/engine/7000.0/script-reference/unityengine/audiorenderer/stop.md) is called.                                              |
| [Stop](/engine/7000.0/script-reference/unityengine/audiorenderer/stop.md)                                                   | Exits audio recording mode. After this audio output will be audible again.                                                                                                                                              |
