sprite-kitskvideonode

play video forever in sprite kit


i am having an issue with playing back a video in my intro scene. i have added my video to the scene and it plays fine. i just want it to repeat again and again. is there any way to set this video to playback automatically after it ends?

this is how i add the video:

SKVideoNode *videoNode = [SKVideoNode videoNodeWithVideoFileNamed:@"game1.m4v"];
videoNode.position = CGPointMake(150, 180);
videoNode.size = CGSizeMake(150, 150);
[self addChild:videoNode];
[videoNode play];

any help is appreciated.


Solution

  • Initialize your SKVideoNode with:

    - (instancetype)initWithAVPlayer:(AVPlayer *)player
    

    When setting up the AVPlayer use:

    avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[avPlayer currentItem]];
    

    this will prevent the player to pause at the end.

    In the notification:

    -(void)playerItemDidReachEnd:(NSNotification *)notification {
        AVPlayerItem *p = [notification object];
        [p seekToTime:kCMTimeZero];
    }
    

    this will rewind the movie.

    (Credit to Bastian for his answer to this question)