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

```
I don't personally have experience with `AVAudioEngine` or `AVAudioMixerNode`, however, you can do this with `Timer`. 

You currently have `audioPlayer.play(at: nil)`, however, this will always play immediately after establishing this player. What I would do, or try to do, is apply some kind of timer to this. 

```
var timeAfter = 10
var timer = NSTimer.scheduledTimerWithInterval(timeAfter, target: self, selector: #selector(playAudio), userInfo: audioPlayer, repeats: false)

```

which would replace 

```
audioPlayer.play(at: nil)

```

Then you would add the function of the timer to play the audio.

```
func playAudio(timer:NSTimer){
  var audioPlayer = timer.userInfo as AVAudioPlayerNode
  audioPlayer.play()
}

```