play video forever in sprite kit

人走茶凉 提交于 2019-11-29 21:35:54

问题


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.


回答1:


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)



来源:https://stackoverflow.com/questions/28814929/play-video-forever-in-sprite-kit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!