Looping AVPlayer seamlessly

后端 未结 4 2223
花落未央
花落未央 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: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)
            }
        }
    

提交回复
热议问题