There has been some discussion before about how to loop an AVPlayer
\'s video item, but no \'solution\' is seamless enough to provide lag-less l
I had the same problem when streaming a video. After playing for the first time, there was a black screen when loading the video for second time. I got rid of the black screen by seeking video to 5ms ahead. It made nearly a seamless video loop. (Swift 2.1)
// Create player here..
let player = AVPlayer(URL: videoURL)
// Add notification block
NSNotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayToEndTimeNotification, object: player.currentItem, queue: nil)
{ notification in
let t1 = CMTimeMake(5, 100);
player.seekToTime(t1)
player.play()
}
You mention that you looked at AVAnimator, but did you see my blog post on this specific subject of seamless looping? I specifically built seamless looping logic in because it could not be done properly with AVPlayer and the H.264 hardware.
I use two AVPlayerItems with the same AVAsset in an AVQueuePlayer and switch the items:
weak var w = self
NSNotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayToEndTimeNotification, object: nil, queue: nil) { (notification) -> Void in
let queuePlayer = w!.playerController.player! as! AVQueuePlayer
if(queuePlayer.currentItem == playerItem1) {
queuePlayer.insertItem(playerItem2, afterItem: nil)
playerItem1.seekToTime(kCMTimeZero)
} else {
queuePlayer.insertItem(playerItem1, afterItem: nil)
playerItem2.seekToTime(kCMTimeZero)
}
}
If the video is very short (a few seconds), you can probably extract each frame as CGImage and use CAKeyframeAnimation to animate it. I am using this technique to play GIF images on my app and the animation is very smooth.