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)
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;
}
else
{
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;
}
else
{
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.