Swift 2.2 - SpriteKit - 使用 SpriteKit 循环播放视频



当我使用 SpriteKit 创建视频对象时

let background = SKVideoNode(videoFileNamed: "Video.mov")

我只得到了一个命令:

background.play()

如何创建一个无限循环以使其一直播放?

我找到了一些关于这个问题的回复,但只在 Objective-C 中,而不是在 Swift 中。我必须使用 AVPlayer() 但是如何?

感谢您的帮助,

我通过编译来自stackoverflow的不同先例响应找到了解决方案。这是工作代码(swift 2.2)。此代码使用场景中的 SKSVideoNode 对象(使用 AVPlayer 初始值设定项)无缝循环播放视频:

override func didMoveToView(view: SKView) {
    let urlStr = NSBundle.mainBundle().pathForResource("Video_Socle", ofType: "mov")
    let url = NSURL(fileURLWithPath: urlStr!)
    player = AVPlayer(URL: url)
 NSNotificationCenter.defaultCenter().addObserverForName(AVPlayerItemDidPlayTo     EndTimeNotification
   , object: player!.currentItem, queue: nil)
    { notification in
        let t1 = CMTimeMake(5, 100);
        self.player!.seekToTime(t1)
        self.player!.play()
    }

    videoNode = SKVideoNode(AVPlayer: player!)
    videoNode!.position = CGPointMake(frame.size.width/2, frame.size.height/2)
    videoNode!.size = CGSize(width: 2048, height: 1536)
    videoNode!.zPosition = 0

    background.addChild(videoNode!)
    videoNode!.play()

最新更新