1

Im using an AVAudioPlayerNode to loop playback of an audio file in buffer and need to be able to switch to the next track when the user clicks next but switch at the end of the loop instead of as soon as the user clicks next

Here's the code I use to start playback

if loopPlayback == true {  
    audioPlayer.scheduleBuffer(buffer, at: nil, options: .loops, completionHandler: nil)
    audioPlayer.play()
    playPauseToggleState()  
}
Lukas Würzburger
  • 6,543
  • 7
  • 41
  • 75
Deji Toki
  • 11
  • 3

1 Answers1

1

Use the AVAudioPlayerNodeBufferOptions.interruptsAtLoop option in the options argument of AVAudioPlayer's scheduleBuffer function. Here is an example:

self.player.scheduleBuffer(self.buffer, at: nil, options: [.interruptsAtLoop, .loops])

This line of code schedules a buffer to be played in loop forever. If there is something already playing, it is interrupted as soon as it finishes a loop iteration. Otherwise, playback starts now. Of course, I'm assuming the player node is currently playing.

Lucas Barbosa
  • 173
  • 2
  • 8