Looping AVPlayer seamlessly

后端 未结 4 2217
花落未央
花落未央 2020-11-29 09:51

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

相关标签:
4条回答
  • 2020-11-29 10:10

    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()
    }
    
    0 讨论(0)
  • 2020-11-29 10:16

    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.

    0 讨论(0)
  • 2020-11-29 10:23

    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)
            }
        }
    
    0 讨论(0)
  • 2020-11-29 10:27

    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.

    0 讨论(0)
提交回复
热议问题