如需更深入的解释,请查看我的答案
AVAudioEngine 多个 AVAudioInputNode 不能完美同步播放
简而言之:
如果您的引擎已经在运行,那么您在 AVAudioNode 中有一个@property lastRenderTime - 您的播放器的超类 - 这是您获得 100% 样本帧准确同步的门票...
AVAudioFormat *outputFormat = [playerA outputFormatForBus:0];
const float kStartDelayTime = 0.0; // seconds - in case you wanna delay the start
AVAudioFramePosition startSampleTime = playerA.lastRenderTime.sampleTime;
AVAudioTime *startTime = [AVAudioTime timeWithSampleTime:(startSampleTime + (kStartDelayTime * outputFormat.sampleRate)) atRate:outputFormat.sampleRate];
[playerA playAtTime: startTime];
[playerB playAtTime: startTime];
[playerC playAtTime: startTime];
[playerD playAtTime: startTime];
[player...
顺便说一句-您可以使用 AVAudioPlayer 类实现相同的 100% 样本帧准确结果...
NSTimeInterval startDelayTime = 0.0; // seconds - in case you wanna delay the start
NSTimeInterval now = playerA.deviceCurrentTime;
NSTimeIntervall startTime = now + startDelayTime;
[playerA playAtTime: startTime];
[playerB playAtTime: startTime];
[playerC playAtTime: startTime];
[playerD playAtTime: startTime];
[player...
如果没有 startDelayTime,所有播放器的前 100-200 毫秒将被剪掉,因为尽管播放器现在已经开始(好吧,被安排)100% 同步,但 start 命令实际上需要时间进入运行循环。但是如果startDelayTime = 0.25,你就可以开始了。并且永远不要忘记提前准备好你的球员,这样在开始时就不需要额外的缓冲或设置 - 只是让他们开始;-)