Misaligned Sound Playback using AVPlayer and AVMutableComposition

I’m trying to create a song from multiple instrument samples using AVComposition.

When I play two sound assets at kCMTimeZero in an AVMutableComposition, I would expect them to play at the same time, but there is a very slight offset.

  • Changing AVCaptureDeviceInput leads to AVAssetWriterStatusFailed
  • AVVideoCompositionCoreAnimationTool and CALayer in portrait mode?
  • Making the iPhone vibrate
  • iPhone Detect Volume Keys press.
  • How to move video from application documents directory to camera roll?
  • AVURLAsset refuses to load video
  • This only happens on the first playthrough, so it would seem to be some kind of loading delay, but no matter what I try it doesn’t go away.

    I’ve made sure to preload the sound asset tracks, preload the composition tracks, wait for the playerItem to be ready, and wait for the player to be ready to play. I’ve tried prerolling the player, play/pausing it, etc. There is always a slight offset between the tracks the first time they’re played.

    I’ve created a small sample project to demonstrate the effect here

    The main code is here

    Note that when you first load the application and hit play, the sounds are slightly offset. Subsequent repeats of the sounds (and replays of the same playerItem) do not have this issue.

    Delaying the initial sounds slightly seems to resolve the issue, but this is not a very nice option.

    Any idea if this can be fixed or if I’m missing something?