AUGraph中多个玩家的同步对局

Synchronous play of several players in AUGraph

我使用 AUGraph,它包含几个(例如 10 个)播放器、混音器和输出。 如果我延迟约 0.2 秒打开玩家 - 没关系,他们都会玩,但如果我同时打开它们,只有 3-4 个玩家玩。

没关系,所有玩家都玩了,但有延迟:

    for(int i=0; i<10; i++){
       [players[i] play];
       usleep(200000);
    }

不行,只有2-3个玩家玩,但是同步:

    for(int i=0; i<10; i++){
       [players[i] play];
    }

[player play]方法中:

   AudioTimeStamp startTime;
   memset (&startTime, 0, sizeof(startTime));
   startTime.mFlags = kAudioTimeStampSampleTimeValid;
   startTime.mSampleTime = -1;

   AudioUnitSetProperty(audioUnit, 
      kAudioUnitProperty_ScheduleStartTimeStamp, 
      kAudioUnitScope_Global, 
      0, 
      &startTime, 
      sizeof(startTime));

AudioUnitSetProperty returns 没有错误。

好像AudioUnitSetProperty和几个线程有冲突,我控制不了。

有什么想法吗?

在 kAudioUnitProperty_ScheduleStartTimeStamp 解决问题之前添加 kAudioUnitProperty_ScheduledFilePrime 设置。

    UInt32 defaultVal = 0;
    AudioUnitSetProperty(audioUnit,
                         kAudioUnitProperty_ScheduledFilePrime,
                         kAudioUnitScope_Global,
                         0,
                         &defaultVal,
                         sizeof(defaultVal));