- Home /
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!
Try using ulong
(UInt64) ins$$anonymous$$d of int
(Int32).
ulong cannot be used as a RPC param... tried to type newOffset as ulong anyway, no difference. Sync is nearly ok now that I've set DSPBufferSize to best latency, but it is not perfect. I get variations of about .05 seconds...
Answer by gregzo · Apr 20, 2012 at 12:49 PM
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!
After further testing, I can't get it to sync without setting the buffer size to best latency (which gives a bufferLength of 256, and numBuffers 4).