Hi guys,
I have tried to wrote a script that basically take the input from the mic and stream it on a audio source. This script works perfectly on local, but when I try to make the object a network object adding network identity and making it spawned by NetworkManager, it stops to work correctly.
const int FREQUENCY = 44100;
AudioClip mic;
int lastPos, pos;
// Use this for initialization
void Start () {
mic = Microphone.Start(null, true, 10, FREQUENCY);
AudioSource audio = GetComponent<AudioSource>();
audio.clip = AudioClip.Create("test", 10 * FREQUENCY, mic.channels, FREQUENCY, false);
audio.loop = true;
}
// Update is called once per frame
void Update () {
if((pos = Microphone.GetPosition(null)) > 0){
if(lastPos > pos) lastPos = 0;
if(pos - lastPos > 0){
// Allocate the space for the sample.
float[] sample = new float[(pos - lastPos) * mic.channels];
// Get the data from microphone.
mic.GetData(sample, lastPos);
// Put the data in the audio source.
AudioSource audio = GetComponent<AudioSource>();
audio.clip.SetData(sample, lastPos);
if(!audio.isPlaying) audio.Play();
lastPos = pos;
}
}
}
void OnDestroy(){
Microphone.End(null);
}
I thought the problem could be related to the network traffic, but update it’s local and as far as I know, no automatic replication is set, so it cannot be the problem, right? Can someone figure out what is the problem?
NB: I know that to assign directly the AudioClip from the microphone is the right way to do it, but it’s not the way I need it.