Stuttering when Playing Audio Streamed from Server

Hello.

I’m writing a VoIP-like system in unity, and I already got the voice data chunks sent from the client to the server, and the server routing it to all the clients.

My client has an infinite clip being played in an AudioSource, like this:

mainClip = AudioClip.Create("voice", 44100, 1, 44100, true, OnAudioRead);

So this is basically a 1-second clip that is looped (and stream is set to true as well), and should be fed data according to the OnAudioRead delegate:

private void OnAudioRead(float[] data)
    {
        Array.Copy(samples.GetRange(0, data.Length).ToArray(), 0, data, 0, data.Length);
        samples.RemoveRange(0, data.Length);
    }

The problem is: whenever I receive the audio and play it, it has a lot of stuttering, every second or so you can hear the sound getting stuck in a loop, sounding like a very poor quality phone call.

I even tried adding some delay to see if the quality improves, but it doesn’t really, you can still hear the audio repeating the samples every now and then.

I already checked the individual chunks of data by recording them into a file, and the audio being played from the file is completely seamless, which leads me to believe there’s either something wrong with the way I’m feeding the data into the clip, or the AudioClip.Create isn’t supposed to be used in this way.

Any ideas?

If anyone wants to know, I eventually figured it out.

Not sure if there’s a better way to do this, but with the help of this article Blog - John Leonard French, I managed to toggle between two audio sources and play a seamless voice stream from the server, using the PlayScheduled method, which has perfect time precision.

Also, I made it so when the client receives audio packets, they combine the packets together into a bigger audio clip (100ms more or less each one). Maybe this is not necessary, but I figured that toggling between very small 25ms clips wouldn’t work (but maybe it does).

I found that setting up a small delay between the first packet received by the client, and actually starting to play the audio, greatly increases the quality too. 200ms did the trick for me.