我正在使用雪碧套件和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];发布于 2014-03-28 11:02:38
我也遇到了类似的问题,就这样解决了:
我将视频的第一帧输出为PNG。然后,我在SKSpriteNode前面添加了一个带有PNG的SKVideoNode。启动视频时,我将SKSpriteNode的SKSpriteNode属性设置为YES。
下面是我的代码的一个简化示例和相关部分:
-(void)didMoveToView:(SKView *)view {
// add first frame on top of the SKVideoNode
firstFrame = [SKSpriteNode spriteNodeWithImageNamed:@"firstFrame"];
firstFrame.zPosition = 1;
[self addChild:firstFrame];
// here I add the SKVideoNode with AVPlayer
......
// Add KVO
[playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == player.currentItem && [keyPath isEqualToString:@"status"]) {
if (player.currentItem.status == AVPlayerItemStatusReadyToPlay) {
[introVideo play];
firstFrame.hidden = YES;
}
}
}这是迄今为止我找到的最好的解决方案。我希望这对你有帮助!
发布于 2014-12-15 14:10:54
有同样的问题。我的解决办法:
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
}发布于 2014-03-25 11:19:07
奇怪的问题..。你能不用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];https://stackoverflow.com/questions/22616190
复制相似问题