Question: I'm trying to mix two audio files in swift AVFoundations. I have a solution that mixes two audio and plays them simultaneously. What I really want is to start the second audio after some time. For example audio 1 plays then second audio starts playing after 10(given time) seconds. Any help is appreciated. Thanks in advance.

```
private var audioFiles: Array<String>
private var audioEngine: AVAudioEngine = AVAudioEngine()
private var mixer: AVAudioMixerNode = AVAudioMixerNode()

func Play() {
    // do work in a background thread
    DispatchQueue.global(qos: .background).async {
      self.audioEngine.attach(self.mixer)
      self.audioEngine.connect(self.mixer, to: self.audioEngine.outputNode, format: nil)
      // !important - start the engine *before* setting up the player nodes
      try! self.audioEngine.start()

      let fileManager = FileManager.default
      for audioFile in self.audioFiles {
        // Create and attach the audioPlayer node for this file
        let audioPlayer = AVAudioPlayerNode()
        self.audioEngine.attach(audioPlayer)
        // Notice the output is the mixer in this case
        self.audioEngine.connect(audioPlayer, to: self.mixer, format: nil)
        let fileUrl = NSURL.init(fileURLWithPath: fileName.removingPercentEncoding!)
        var file : AVAudioFile

        // We should probably check if the file exists here ¯\_(ツ)_/¯
        try! AVAudioFile.init(forReading: fileUrl.absoluteURL!)

        audioPlayer.scheduleFile(file, at: nil, completionHandler: nil)
        audioPlayer.play(at: nil)
      }
    }
  }

```

Answer: 
You should use [scheduleSegment](https://developer.apple.com/documentation/avfoundation/avaudioplayernode/1385884-schedulesegment).

```
func scheduleWithOffset(_ offset: TimeInterval) {

    let samplerate1 = file1.processingFormat.sampleRate
    player1.scheduleSegment(file1,
                            startingFrame: 0,
                            frameCount: AVAudioFrameCount(file1.length),
                            at: AVAudioTime(sampleTime: 0, atRate: samplerate1))

    let samplerate2 = file2.processingFormat.sampleRate
    player2.scheduleSegment(file2,
                            startingFrame: 0,
                            frameCount: AVAudioFrameCount(file2.length),
                            at: AVAudioTime(sampleTime: AVAudioFramePosition(offset * samplerate2), atRate: samplerate2))

    //This can take an indeterminate amount of time, so both files should be prepared before either starts.
    player1.prepare(withFrameCount: 8192)
    player2.prepare(withFrameCount: 8192)

    // Start the files at common time slightly in the future to ensure a synchronous start.
    let hostTimeNow = mach_absolute_time()
    let hostTimeFuture = hostTimeNow + AVAudioTime.hostTime(forSeconds: 0.2);
    let startTime = AVAudioTime(hostTime: hostTimeFuture)

    player1.play(at: startTime)
    player2.play(at: startTime)
}

```
You may add another playerNode to the mixer and then play it after delay like this :

```
    let audioPlayer2 = AVAudioPlayerNode()
    self.audioEngine.attach(audioPlayer2)
    self.audioEngine.connect(audioPlayer2, to: self.mixer, format: nil)
    var file2 : AVAudioFile = try! AVAudioFile.init(forReading: fileUrl2!)
    func delayTime(_ delayTime : TimeInterval) -> AVAudioTime{
        let  outputFormat = audioPlayer2.outputFormat(forBus: 0)
        let  startSampleTime = (audioPlayer2.lastRenderTime ?? AVAudioTime()).sampleTime +  Int64(Double(delayTime) * outputFormat.sampleRate);
        return  AVAudioTime(sampleTime: startSampleTime, atRate: outputFormat.sampleRate)
    }
    audioPlayer2.scheduleFile(file2, at: nil, completionHandler: nil)
    audioPlayer2.play(at: delayTime(3.0))

```

You can also hook a sampleDelay AU between node2 and mixer. That makes things easier without programming manually.