检测本地视频何时结束并移至另一个场景

时间:2018-10-25 11:35:01

标签: ios swift sprite-kit avplayer skscene

如何检测本地视频已结束并移至另一个场景?

这是代码:

import SpriteKit
import GameplayKit
import AVKit
import AVFoundation

class intro: SKScene {

  override func didMove(to view: SKView) {
      let urlStr = Bundle.main.path(forResource: "Opening", ofType: "mp4")
      let url = NSURL(fileURLWithPath: urlStr!)

      let player = AVPlayer(url: url as URL)

      let videoNode = SKVideoNode(avPlayer: player)
      videoNode.position = CGPoint(x: 0, y: 0)
      videoNode.size = CGSize(width: self.frame.width, height: self.frame.height)
      videoNode.zPosition = 1

      addChild(videoNode)
      videoNode.play()
  }

  override func update(_ currentTime: TimeInterval) {
      // Called before each frame is rendered
  }
}

视频结束后,我需要知道如何移动到另一个场景。 谢谢。

1 个答案:

答案 0 :(得分:0)

您可以像这样触发通知:

  NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying),
                                               name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                                               object: self.player!.currentItem)



 @objc func playerDidFinishPlaying(){
    print("Player finished")
}