How do I control playback of a video in an iOS Sprite Kit SKVideoNode?

那年仲夏 提交于 2019-12-09 02:04:39

问题


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];

回答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;



回答2:


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?




回答3:


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



来源:https://stackoverflow.com/questions/27027648/how-do-i-control-playback-of-a-video-in-an-ios-sprite-kit-skvideonode

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