Play Looping Video using Swift

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

<span>%d</span> bloggers like this: