Synching audioclips over a network - why is this not working?

Hi to all! Trying to get a clip to play in sync on two networked devices.

Here’s my script:

function SyncPlay(offset:int)
{
	networkView.RPC ("Play", RPCMode.Others,offset);
	
	audio.Play(offset);
}

@RPC
function Play(offset:int, info : NetworkMessageInfo)
{
	var timeInTransit : float = Network.time-info.timestamp;
	Debug.Log("timeInTransit : "+timeInTransit);
	var newOffset : int = offset - 44100.0*timeInTransit;
	Debug.Log("newOffset : "+newOffset);
	audio.Play(newOffset);
}

I’m launching SyncPlay(44100) from a GUI button on a mac (which is the server), the connected device is an iPad. timeInTransit and newOffset Debug show absolutely coherent values (.1 s, newOffset 33000 for example), but the audio is not in sync (the server plays about .1s early).

Any ideas? Thanks!

Got it, yippee!!!

  1. Project settings > Quality > vSync off

  2. Application.targetFrameRate = 30; (in my case, just to make sure the frame rate on the server is as close as possible to the device’s)

  3. Add a yieldForEndOfFrame befor playing but after the RPC call:

    function SyncPlay(offset:int)
    {
    networkView.RPC (“Play”, RPCMode.Others,offset);
    yield WaitForEndOfFrame;
    audio.Play(offset);
    }

  4. Might still need a little adjustment in the Play function. In my case,

    var newOffset : int = offset - 44100.0*timeInTransit -256;

gives a perfect result. 256 happens to be the DSP buffer size… need more testing before I confirm the link.

Sync is now stable, happiness!