I need to create something like an infinite loop in my AVQueuePlayer
. Especially, I want to replay the whole NSArray
of AVPlayerItem
s
In Swift 4, you can use something like the following:
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) {
notification in
print("A PlayerItem just finished playing !")
currentVideo += 1
if(currentVideo >= videoPaths.count) {
currentVideo = 0
}
let url = URL(fileURLWithPath:videoPaths[currentVideo])
let playerItem = AVPlayerItem.init(url: url)
player.insert(playerItem, after: nil)
}
From what I understand the AVQueuePlayer
will remove the last item played, so you need to keep adding the video just played to the queue, otherwise it will actually run out of videos to play! You can't just .seek
to 0 and .play()
again, that doesn't work.