Version: Unity 7.0 Alpha (7000.0)
LanguageEnglish
  • C#

AudioRenderer

class in UnityEngine

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Records the main audio output of the application.

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

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
GetSampleCountForCaptureFrameReturns the number of sample frames available since the last time AudioRenderer.Render was called. This count depends on the frame capture rate.
RenderWrites the audio rendered since the previous call into the supplied buffer.
StartEnters audio recording mode. After this Unity will output silence until AudioRenderer.Stop is called.
StopExits audio recording mode. After this audio output will be audible again.