How to synchronize an audio on 2 or more ios devices

后端 未结 2 1700
星月不相逢
星月不相逢 2021-02-04 20:02

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

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-04 20:39

    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).

    enter image description here

    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.

提交回复
热议问题