Playing a looping video in iOS is fairly simple but there’s a catch. You need to declare AVPlayerLooper
global in your class.
Below is the code:
let avPlayerItem = AVPlayerItem(url: videoOutputURL!)
let avQueuePlayer = AVQueuePlayer(playerItem: avPlayerItem)
let avPlayerLayer = AVPlayerLayer(player: avQueuePlayer)
avPlayerLooper = AVPlayerLooper(player: avQueuePlayer, templateItem: avPlayerItem)
avPlayerLayer.frame = videoPlayContainerView.bounds
avPlayerLayer.videoGravity = .resizeAspectFill
avQueuePlayer.actionAtItemEnd = .none
videoPlayContainerView.layer.addSublayer(avPlayerLayer)
avQueuePlayer.play()
The videoPlayContainerView
is a UIView
.