Microphone Input to Spawn object
I'm following a tutorial on this site here: **http://www.kaappine.fi/tutorials/using-microphone-input-in-unity3d/** But the code is out of date and brings bace the following errors after Unity Upgraded it:
Assets/Scripts/MicrophoneInput.cs(13,49): error CS0103: The name AudioInputDevice' does not exist in the current context Assets/Scripts/MicrophoneInput.cs(13,37): error CS1502: The best overloaded method match for
UnityEngine.Microphone.GetPosition(string)' has some invalid arguments
Assets/Scripts/MicrophoneInput.cs(13,37): error CS1503: Argument #1' cannot convert
object' expression to type `string'
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class MicrophoneInput : MonoBehaviour {
public float sensitivity = 100;
public float loudness = 0;
void Start() {
GetComponent<AudioSource>().clip = Microphone.Start(null, true, 10, 44100);
GetComponent<AudioSource>().loop = true; // Set the AudioClip to loop
GetComponent<AudioSource>().mute = true; // Mute the sound, we don't want the player to hear it
while (!(Microphone.GetPosition(AudioInputDevice) > 0)){} // Wait until the recording has started
GetComponent<AudioSource>().Play(); // Play the audio source!
}
void Update(){
loudness = GetAveragedVolume() * sensitivity;
}
float GetAveragedVolume()
{
float[] data = new float[256];
float a = 0;
GetComponent<AudioSource>().GetOutputData(data,0);
foreach(float s in data)
{
a += Mathf.Abs(s);
}
return a/256;
}
}
Answer by vintar · Jan 04, 2016 at 06:02 PM
I think you should use "Microphone" instead of "AudioInputDevice"
@vintar, I changed that and I still get these errors:
Assets/Scripts/$$anonymous$$icrophoneInput.cs(13,37): error CS1503: Argument #1' cannot convert
object' expression to type string' Assets/Scripts/$$anonymous$$icrophoneInput.cs(13,37): error CS1502: The best overloaded method match for
UnityEngine.$$anonymous$$icrophone.GetPosition(string)' has some invalid arguments
Assets/Scripts/$$anonymous$$icrophoneInput.cs(13,49): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected
I REALLY hate that unity keeps mucking about with code updates, it's tough enough trying to learn this stuff when they keep changing how it's written every freaking time they do a blasted update to the software.
Answer by Yword · Jan 05, 2016 at 04:09 AM
Maybe you should pass the device name or null(default device) for Microphone.GetPosition:
Microphone.GetPosition(null)