1

I load a video into my Sprite Kit SKVideoNode, but how do I stop and restart the playback or go to a specific time in the video? All I see are play and pause methods.

// AVPlayer
NSURL *fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"HowToPlay" ofType:@"mp4"]];
_avPlayer = [AVPlayer playerWithURL:fileURL];

// SKVideoNode
_videoNode = [SKVideoNode videoNodeWithAVPlayer:_avPlayer];
[_videoNode play];
lox
  • 1,602
  • 4
  • 27
  • 41

3 Answers3

1

This turned out to work for me.

Restart:

[_videoNode removeFromParent];
 _videoNode = [SKVideoNode videoNodeWithVideoFileNamed:@"video.mp4"];
 _videoNode.position = ...
_videoNode.size = ...
[self addChild:_videoNode];
[_videoNode play];

Pause:

[_videoNode pause];
_videoNode.paused = YES;

Play:

[_videoNode play];
_videoNode.paused = NO;
lox
  • 1,602
  • 4
  • 27
  • 41
0

All you can do with a SKVideoNode is documented here. play and pause are the only supported playback methods.

There is no stop because its equivalent to pause in this context - what should "stop" do, render a black texture? If the SKVideoNode should be removed on "stop" then just send it the removeFromParent message, that'll "stop" it.

Rewind is unnecessary because you can simply run the play method again or create a new SKVideoNode.

I assume jumping to a specific timeframe isn't supported because this can't be instant, so again there's the problem of what the node should display in the time until the video playback position was moved to the specified timestamp?

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Is that from experience or are you just guessing? - It acts very differently for me. When the video is playing, running the play method again has no effect whatsoever. I can pause with the pause command and unpause with the play command, but thats it. Doing a removeFromParent, just makes is invisible, it is still playing and the sound can be heard. – lox Nov 20 '14 at 18:24
  • Based on documentation mostly, and a few experiments. That it keeps playing after removing sounds like a bug, perhaps the node is still strongly referenced somewhere? Try playing with SKVideoNode in a Sprite Kit template project to observe its behavior in isolation. – CodeSmile Nov 20 '14 at 18:53
0

If you keep a reference to the AVPlayer object used to initialize the video node then you can control playback with its methods

  • If I throw commands at the AVPlayer reference they have absolutely no effect. Only commands to the video node are doing anything. – lox Nov 20 '14 at 18:34
  • 2
    That does sound like a bug because the docs clearly state otherwise. – uchuugaka Dec 14 '14 at 11:48