Hi, I’m trying to use microphone input in my program.
Here’s the code that’s calling the Microphone class
public void StartMicrophone()
if (Microphone.devices.Length > 0)
microphoneDevice = Microphone.devices[0];
microphoneClip = Microphone.Start(microphoneDevice, true, 2, frequency);
Debug.LogWarning("No microphone device found.");
It works perfectly when set to Windows. When I switch the platform to Android, it just doesn’t pick up input. This problem persists both in the editor (while on Android platform), and on multiple standalone devices.
Things I’ve tried & verified:
All devices were prompted to accept the permission to have microphone access, and I’ve double checked all have the permission
I’ve verified that an actual input device exists on each device and set the correct one in my script (every device only has 1 anyways)
I’m not getting any errors in my logcat
Not quite sure what the problem could be.
I see a few pitfalls here:
Check that the frequency value is the right one. You can validate you microphone’s capabilities with Microphone.GetDeviceCaps()
If another application is using the microphone, you might simply not be able to access it
You might need to request the permission to use the microphone:
if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
Permission.RequestUserPermission(Permission.Microphone);
I hope this helps!
Hello, i have a similar problem.
I’m working on a decibel meter app for android.
So it needs to use the mic of the android device. I did add the uses permission RECORD_AUDIO in the androidmanifest.xml file.
The first (clean install) on my phone, the mic request works, but the microphone is not starting. Now when i exit and start the app again, the microphone works. This is the script i’m using:
using UnityEngine;
using UnityEngine.Android;
using System.Collections;
public class DecibelMeter : MonoBehaviour
private AudioClip recordedClip;
private AudioSource audioSource;
private bool isRecording = false;
IEnumerator Start()
// Check if a microphone is available
if (Microphone.devices.Length == 0)
Debug.LogError("No microphone detected.");
yield break;
// Request and check microphone permissions
RequestMicrophonePermission();
yield return StartCoroutine(WaitForMicrophonePermission());
Debug.Log("Microphone access granted.");
// Add an AudioSource component to this GameObject
audioSource = gameObject.AddComponent<AudioSource>();
// Wait for a short delay before proceeding
yield return new WaitForSeconds(1);
// Start recording
StartRecording();
void RequestMicrophonePermission()
if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
Permission.RequestUserPermission(Permission.Microphone);
IEnumerator WaitForMicrophonePermission()
// Wait until the user grants or denies permission
while (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
yield return null;
// Start recording audio (changed to public)
public void StartRecording()
if (Microphone.IsRecording(null))
Debug.LogWarning("Already recording.");
return;
int frequency = 44100; // Sample rate
int seconds = 10; // Maximum recording duration
recordedClip = Microphone.Start(null, true, seconds, frequency);
isRecording = true;
// Stop recording audio
public void StopRecording()
if (Microphone.IsRecording(null))
Microphone.End(null);
isRecording = false;
Debug.LogWarning("Not currently recording.");
// Get the current decibel level and play the recorded audio
public float GetDecibelLevel()
if (isRecording && recordedClip != null)
float[] samples = new float[recordedClip.samples * recordedClip.channels];
recordedClip.GetData(samples, 0);
float sum = 0f;
foreach (float sample in samples)
sum += Mathf.Abs(sample);
// Calculate average amplitude
float averageAmplitude = sum / samples.Length;
// Convert average amplitude to decibels
float dbValue = 20 * Mathf.Log10(averageAmplitude + Mathf.Epsilon); // Add epsilon to prevent log of zero
// Play the recorded audio if it's not already playing
if (!audioSource.isPlaying)
audioSource.clip = recordedClip;
audioSource.Play();
return dbValue;
return 0f; // Return 0 if not recording or no recorded audio
class DecibelMeter : MonoBehaviour
private AudioClip recordedClip;
private AudioSource audioSource;
private bool isRecording = false;
IEnumerator Start()
// Check if a microphone is available
if (Microphone.devices.Length == 0)
Debug.LogError("No microphone detected.");
yield break;
// Request and check microphone permissions
RequestMicrophonePermission();
yield return StartCoroutine(WaitForMicrophonePermission());
Debug.Log("Microphone access granted.");
// Add an AudioSource component to this GameObject
audioSource = gameObject.AddComponent<AudioSource>();
// Wait for a short delay before proceeding
yield return new WaitForSeconds(1);
// Start recording
StartRecording();
void RequestMicrophonePermission()
if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
Permission.RequestUserPermission(Permission.Microphone);
IEnumerator WaitForMicrophonePermission()
// Wait until the user grants or denies permission
while (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
yield return null;
// Start recording audio (changed to public)
public void StartRecording()
if (Microphone.IsRecording(null))
Debug.LogWarning("Already recording.");
return;
int frequency = 44100; // Sample rate
int seconds = 10; // Maximum recording duration
recordedClip = Microphone.Start(null, true, seconds, frequency);
isRecording = true;
// Stop recording audio
public void StopRecording()
if (Microphone.IsRecording(null))
Microphone.End(null);
isRecording = false;
Debug.LogWarning("Not currently recording.");
// Get the current decibel level and play the recorded audio
public float GetDecibelLevel()
if (isRecording && recordedClip != null)
float[] samples = new float[recordedClip.samples * recordedClip.channels];
recordedClip.GetData(samples, 0);
float sum = 0f;
foreach (float sample in samples)
sum += Mathf.Abs(sample);
// Calculate average amplitude
float averageAmplitude = sum / samples.Length;
// Convert average amplitude to decibels
float dbValue = 20 * Mathf.Log10(averageAmplitude + Mathf.Epsilon); // Add epsilon to prevent log of zero
// Play the recorded audio if it's not already playing
if (!audioSource.isPlaying)
audioSource.clip = recordedClip;
audioSource.Play();
return dbValue;
return 0f; // Return 0 if not recording or no recorded audio
Unity 2019.4
Btw. It does work immidiately in unity Editor.
Can someone help me out?
What am i doing wrong?
And yes, i’m a noob.
I’ve experienced this myself but didn’t have much time to get into it… mobile permission stuff can be super tricky.
You could try to stop/start the microphone in StartRecording() by calling Microphone.End(null); before Microphone.Start(...);.
Also, instead of a second corountine polling for when the permission has been granted, your could try using the PermissionCallbacks.