我使用Sprite Kit和SKVideoNode播放短视频。当视频节点添加到场景中时,在初始设置之前,播放器会在短时间内闪烁黑色。
我尝试过直接使用AVPlayer代替视频,而且我已经使用KVO只在视频显示已经准备好播放时加载SKVideoNode。
无论哪种方式,我的视频开始播放前都会出现黑色闪光。
此外,似乎没有一种方法可以将AVPlayerLayer添加到SKScene / SKView中,但我不知道这是否会有所帮助。
关于接下来要尝试什么的任何建议都会很棒。
这是我使用
的代码- (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self createSceneContents];
}
}
- (void)createSceneContents
{
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"IntroMovie" ofType:@"mov"];
NSURL *introVideoURL = [NSURL fileURLWithPath:resourcePath];
self.playerItem = [AVPlayerItem playerItemWithURL:introVideoURL];
self.player = [[AVPlayer alloc] initWithPlayerItem:self.playerItem];
[self.playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
SKSpriteNode *playButton = [SKSpriteNode spriteNodeWithImageNamed:@"PlayButton"];
[playButton setPosition:CGPointMake(CGRectGetMidX(self.view.frame), (CGRectGetMidY(self.view.frame) / 5) * 3)];
[self addChild:playButton];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"status"]) {
if ([[change objectForKey:NSKeyValueChangeNewKey] integerValue] == AVPlayerItemStatusReadyToPlay) {
NSLog(@"Change has the following %@", change);
[self.player prerollAtRate:0 completionHandler:^(BOOL done){
SKVideoNode *introVideo = [SKVideoNode videoNodeWithAVPlayer:self.player];
[introVideo setSize:CGSizeMake(self.size.width, self.size.height)];
[introVideo setPosition:self.view.center];
[self addChild:introVideo];
[introVideo play];
[self.playerItem removeObserver:self forKeyPath:@"status"];
}];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
这是另一段代码,它只播放视频,并在正在加载的视频和正在播放的视频之间提供相同的黑色闪光。
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"IntroMovie" ofType:@"m4v"];
NSURL *introVideoURL = [NSURL fileURLWithPath:resourcePath];
self.playerItem = [AVPlayerItem playerItemWithURL:introVideoURL];
self.player = [[AVPlayer alloc] initWithPlayerItem:self.playerItem];
SKVideoNode *introVideo = [SKVideoNode videoNodeWithAVPlayer:self.player];
[introVideo setSize:CGSizeMake(self.size.width, self.size.height)];
[introVideo setPosition:self.view.center];
[self addChild:introVideo];
[introVideo play];
答案 0 :(得分:1)
我遇到了类似的问题并解决了这个问题:
我将视频的第一帧导出为PNG。然后我在SKSpriteNode
前面添加了SKVideoNode
个PNG。当我开始播放视频时,我将hidden
的{{1}}属性设置为SKSpriteNode
。
以下是我的代码的简化示例以及相关部分:
YES
这是迄今为止我发现的最佳解决方案。我希望能帮到你!
答案 1 :(得分:1)
遇到同样的问题。 我的解决方法:
videoNode = SKVideoNode(AVPlayer: player)
videoNode.hidden = true
player.addBoundaryTimeObserverForTimes([1/30.0], queue: dispatch_get_main_queue()) { [unowned self] () -> Void in
self.videoNode.hidden = false
}
答案 2 :(得分:0)
奇怪的问题......你能不试用AVPlayerItem
吗?像这样:
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"IntroMovie"
ofType:@"m4v"];
NSURL *introVideoURL = [NSURL fileURLWithPath:resourcePath];
AVPlayer *player = [AVPlayer playerWithURL: introVideoURL];
SKVideoNode *introVideo = [[SKVideoNode alloc] initWithAVPlayer: player];
[introVideo setSize:CGSizeMake(self.size.width, self.size.height)];
[introVideo setPosition:self.view.center];
[self addChild:introVideo];
[introVideo play];