AudioRenderer
Records the main audio output of the application.
Read time 2 minutesLast updated 3 days ago
Definition
- Type: Class
- Namespace: UnityEngine
- Assembly: UnityEngine.AudioModule
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 to enter recording mode. While recording, Unity outputs silence to the audio device and delivers the mixed samples through AudioRenderer.Render instead. Call AudioRenderer.Stop to leave recording mode and make audio audible again.
Call AudioRenderer.Render once per frame while recording. Use AudioRenderer.GetSampleCountForCaptureFrame 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, AudioSpeakerMode
Examples
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 | Returns the number of sample frames available since the last time AudioRenderer.Render was called. This count depends on the frame capture rate. |
| Render | Writes the audio rendered since the previous call into the supplied buffer. |
| Start | Enters audio recording mode. After this Unity will output silence until AudioRenderer.Stop is called. |
| Stop | Exits audio recording mode. After this audio output will be audible again. |