如何在2个或更多ios设备上同步音频 [英] How to synchronize an audio on 2 or more ios devices

查看:350
本文介绍了如何在2个或更多ios设备上同步音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在给定的时间点执行动作(在多个设备上一次播放歌曲而没有延迟)。我的要求是应用程序不应使用任何互联网连接。所以我需要一个准确的时间来播放数据(音频)]

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. So I need an exact point of time when to play data(Audio)]

我已经尝试过以下:


  1. 在所有设备上播放5秒钟后的歌曲。 (仍然导致延迟毫秒)

  2. 发送一个小文本通知以识别歌曲的播放,然后开始在所有设备上播放。但是发送和接收此通知需要花费几毫秒的时间:(:(

  3. 在所有设备上设置自动时间,然后检查文本消息传递的时间差,每次测试都有毫秒差距。


推荐答案

首先你需要同步2台设备的内部时间。
显然是iOS上最准确的时钟就是绝对时间:

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();

然后您需要将此时间从设备 A 发送到设备 B 检索设备上的差异 B 并将其发送回 A ,然后将差异与网络事务中花费的时间进行比较。通过这种方式,您可以非常准确地同步2个设备的时间。(假设网络时间在请求和响应之间是对称的,并且没有你的开销很大r在设备上计算 B )。

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

从那里你可以轻松实例化2 AVAudioPlayer 在设备上设置歌曲并调用 prepareToPlay ,最后根据同步延迟激活播放方法。
这应该给你一个不明显的精确度。

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.

你可以采取的另一种方法是放置一个 AudioUnit ,以便根据第一个设备发出的声音同步音乐。

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.

这篇关于如何在2个或更多ios设备上同步音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆