I have to perform an action (play song at a time on multiple devices without lag) at a given point of time. My requirement is that the app should not use any internet connection
Known Issue
When sleeping or waiting for extremely precise time intervals, timers may be delayed by up to 1 millisecond.
For reference : https://developer.apple.com/library/ios/releasenotes/General/RN-iOSSDK-7.0/
First you need to synchronize the 2 devices internal time. Apparently the most accurate clock you can have on iOS is the absolute time by doing :
CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
Then you need to send this time from device A
to device B
retrieve the difference on device B
and send it back to A
, then compare the difference with the time spent in the network transaction. In this way you can have a quite accurate synchronisation of the 2 devices time. (This is presuming the network time is symmetric between request and response and that there is not much overhead on your calculation on device B
).
From there you can easily instantiate 2 AVAudioPlayer
on the devices set the song and call prepareToPlay
and finally fire the play method according to the delay on the synch.
This should give you a not noticeable precision.
Another route you can take is the one of placing an AudioUnit
on the second device connected to the microphone in order to sync the music accordingly to the first device emitted sound.