I have an app which plays a series of videos for the user, with optional looping of the series. My view has an AVPlayerViewController attached to it, and I initiate and control playback by passing an array of items to these two functions:
private func play(_ items: [Item], loopAll: Bool) throws {
let player = AVQueuePlayer()
playerController = AVPlayerViewController()
playerController!.player = player
self.present(playerController!, animated: true) {
for item in items {
self.addItemToQueue(player: player, item: item, loopAll: loopAll)
}
player.actionAtItemEnd = .advance
player.play()
}
}
private func addItemToQueue(player: AVQueuePlayer, item: Item, loopAll: Bool) {
let playerItem = AVPlayerItem(url: item.videoUrl)
player.insert(playerItem, after: player.items().last)
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem, queue: nil) {
note in
if (loopAll) {
self.addItemToQueue(player: player, item: item, loopAll: loopAll)
}
}
}
This works great, with the exception of the forward/back buttons in the AVPlayerViewController. Play/Pause, Done, and scrubbing work as expected, but tapping either Forward or Back has no effect. How can I enable those buttons to actually call player.advanceToNextItem, or worst case, hide them so the user doesn't see confusing/useless buttons during playback?