How do I create a looping video material in SceneKit on iOS in Swift 3?

前端 未结 3 2040
甜味超标
甜味超标 2021-02-06 08:16

How do I create a material in SceneKit that plays a looping video?

3条回答
  •  星月不相逢
    2021-02-06 08:35

    Year 2019 solution:

    let mat = SCNMaterial()
    let videoUrl = Bundle.main.url(forResource: "YourVideo", withExtension: "mp4")!
    let player = AVPlayer(url: videoUrl)
    mat.diffuse.contents = player
    player.actionAtItemEnd = .none
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(playerItemDidReachEnd(notification:)),
                                           name: .AVPlayerItemDidPlayToEndTime,
                                           object: player.currentItem)
    player.play()
    

    Code for method in selector:

    @objc private func playerItemDidReachEnd(notification: Notification) {
        if let playerItem = notification.object as? AVPlayerItem {
            playerItem.seek(to: .zero, completionHandler: nil)
        }
    }
    

    Don't forget to remove your notification observer when the object is deallocated! Something like this:

    NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: player.currentItem)
    

提交回复
热议问题