Simple question I can\'t seem to find an answer to for some reason.
How do you loop AVPlayer in Swift?
numberOfLoops = -1 only works for AVAudioPlayer
<Starting from iOS 10, there's no need to use notifications to loop a video, simply use a AVPlayerLooper, just like this:
let asset = AVAsset(url: videoURL)
let item = AVPlayerItem(asset: asset)
let player = AVQueuePlayer(playerItem: item)
videoLooper = AVPlayerLooper(player: player, templateItem: item)
make sure this looper is created outside of a function scope, in case it stops when function returns.
Swift 3.0:
First check for video end point:-
NotificationCenter.default.addObserver(self, selector: #selector(LoginViewController.playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)
func videoDidReachEnd() {
//now use seek to make current playback time to the specified time in this case (O)
let duration : Int64 = 0 (can be Int32 also)
let preferredTimeScale : Int32 = 1
let seekTime : CMTime = CMTimeMake(duration, preferredTimeScale)
player!.seek(to: seekTime)
player!.play()
}