How to detect SKVideoNode playback completion

420 Views Asked by At

I am using an SKVideoNode to display animated splash scene using this code:

    SKVideoNode *video = [SKVideoNode videoNodeWithVideoFileNamed:@"splash_video.mp4"];



    video.position = CGPointMake(CGRectGetMidX(self.frame),
                                 CGRectGetMidY(self.frame));

    [self addChild:video];
    [video play];

I wish to display the next scene once video playback is done. How can I achieve that?

1

There are 1 best solutions below

0
On

Add this to your viewDidLoad or elsewhere suitable

let videoURL = NSURL(string: url)
let player = AVPlayer(URL: videoURL!)
player.actionAtItemEnd = .None
let videoSpriteKitNode = SKVideoNode(AVPlayer: player)

NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:", name: AVPlayerItemDidPlayToEndTimeNotification, object: player.currentItem)

And add this function:

func playerItemDidReachEnd(notification: NSNotification) {

    if let playerItem = notification.object as? AVPlayerItem {
        //Start your next video here
    }
}