- Home /
Updated to Unity 5.2 and suddenly my mic input script isn't working?
I have this mic input script I'm using for a game of mine that I learned online from http://www.kaappine.fi/tutorials/using-microphone-input-in-unity3d/ it's pretty straight forward and I'll post the code below, now this script was working wonderfully and I've built a whole game around it using it as the main backbone if you will, after I updated unity to fix the OnApplicationPause() problem I was having the loudness variable now suddenly stays at zero. I use the loudness variable to compare to a threshold to spawn my objects, so you see my problem. The OnApplicationPause now works though *frustrated face. PLEASE HELP
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class MicrophoneInput : MonoBehaviour {
public float sensitivity = 100f;
public float loudness = 0f;
// Use this for initialization
void Start () {
GetComponent<AudioSource>().clip = Microphone.Start (null, true, 10, 44100);
GetComponent<AudioSource>().loop = true;
GetComponent<AudioSource>().mute = true;
while (!(Microphone.GetPosition(null) > 0)){}
GetComponent<AudioSource>().Play ();
}
// Update is called once per frame
void Update () {
loudness = GetAveragedVolume () * sensitivity;
}
float GetAveragedVolume(){
float[] data = new float[256];
float a = 0f;
GetComponent<AudioSource>().GetOutputData (data, 0);
foreach(float s in data)
{
a += Mathf.Abs(s);
}
return a/256;
}
}
Answer by jamal-dahbur · Oct 18, 2015 at 05:59 PM
Ok so after doing some extensive research I found out I wasn't the only one having this problem and I found a solution, the problem was with part of the code that mutes the audio source, apparently a mute audio source sending output data was an error during the older versions of unity that they actually fixed this version, and if you want to get the output data the audio source needs to be un-muted but you can achieve the same effect by routing the audio source through and audio mixer group that is fully attenuated.
And here's how to do the Audio $$anonymous$$ixer part: https://unity3d.com/learn/tutorials/modules/beginner/5-pre-order-beta/audiomixer-and-audiomixer-groups
Your answer
Follow this Question
Related Questions
About Android's MicPhone input 0 Answers
compress recorded audio in an app? 0 Answers
Multiple Audiosources recording from the same microphone 3 Answers
sound IOS recording 1 Answer
How yo stream mic data to web using UnityWebRequest? 1 Answer