Use a looping AudioClip from Microphone.Start(), wait for Microphone.GetPosition() to exceed zero, then play it through an Audio Source to reduce latency. Add platform permissions; complex voice systems may require dedicated solutions.
To play back microphone input in real time, record into a looping AudioClip using Microphone.Start() and use Microphone.GetPosition() to control when playback begins in order to minimize latency.
What problems can occur during microphone playback?
You may experience one or more of the following:
- Choppy or inaudible audio during playback
- Audio playing back in an infinite feedback loop
- Noticeable latency — audio does not play back immediately
How do I set up real-time microphone playback?
- Configure the Audio Source:
- Set the microphone as an AudioClip and attach it to an Audio Source component.
- Enable Loop on the Audio Source in the Inspector.
- Use spectrum data from the Audio Source, not from the Audio Listener.
- Attach a script to the Audio Source that calls
Microphone.Start()to begin recording. - Call
Microphone.GetPosition()to determine the current recording position. To start playback with minimal latency, wait until the microphone position is greater than zero before callingaudioSource.Play().
using System.Collections;
using UnityEngine;
public class MicrophonePlayback : MonoBehaviour
{
IEnumerator Start()
{
AudioSource audio = GetComponent<AudioSource>();
audio.clip = Microphone.Start(null, true, 10, AudioSettings.outputSampleRate);
audio.loop = true;
while (Microphone.GetPosition(null) <= 0)
{
yield return null;
}
audio.Play();
}
}What permissions do I need?
You must add microphone permissions to your app on platforms that require it (iOS, Android). Permissions are added automatically if your script contains a reference to UnityEngine.Microphone.
On Android specifically, adding the microphone permission is not strictly required, as it breaks compatibility with Android TV.
What are the limitations for complex use cases?
For complex real-time audio systems such as voice chat, Unity's built-in Microphone class may not be sufficient. Consider using dedicated solutions:
- NAudio — a low-level audio library for .NET
- Agora — a real-time communication framework
- Dissonance Voice Chat — available on the Unity Asset Store
- Vivox — Unity's own voice and text chat solution