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 )

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

Connecting to %s

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close