首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在SKVideoNode中播放视频会导致播放开始前出现黑色闪光灯。

在SKVideoNode中播放视频会导致播放开始前出现黑色闪光灯。
EN

Stack Overflow用户
提问于 2014-03-24 17:16:30
回答 3查看 2K关注 0票数 5

我正在使用雪碧套件和SKVideoNode播放一个简短的视频。当视频节点被添加到场景中时,是在初始设置之后的场景之前,播放机会闪烁一段时间的黑色。

我已经尝试使用AVPlayer来代替直接的视频,我也使用KVO只在视频显示它可以播放的时候加载SKVideoNode。

不管怎样,在我的视频开始播放之前,我会得到一个黑色的闪光灯。

而且,似乎没有一种方法可以将AVPlayerLayer添加到SKScene/SKView中,尽管我不知道这是否有帮助。

任何关于下一步该尝试什么的建议都会很棒。

这是我正在使用的代码

代码语言:javascript
复制
- (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];
    }
}

这是另一段代码,它只播放视频,在加载的视频和播放的视频之间发出相同的黑色闪光灯。

代码语言:javascript
复制
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];
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-03-28 11:02:38

我也遇到了类似的问题,就这样解决了:

我将视频的第一帧输出为PNG。然后,我在SKSpriteNode前面添加了一个带有PNG的SKVideoNode。启动视频时,我将SKSpriteNodeSKSpriteNode属性设置为YES

下面是我的代码的一个简化示例和相关部分:

代码语言:javascript
复制
-(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;             
        }
    }
}

这是迄今为止我找到的最好的解决方案。我希望这对你有帮助!

票数 1
EN

Stack Overflow用户

发布于 2014-12-15 14:10:54

有同样的问题。我的解决办法:

代码语言:javascript
复制
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
}
票数 1
EN

Stack Overflow用户

发布于 2014-03-25 11:19:07

奇怪的问题..。你能不用AVPlayerItem试试吗?就像这样:

代码语言:javascript
复制
  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];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22616190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档