在开始播放之前,在SKVideoNode中播放视频会导致黑色闪烁 [英] Playing back video in an SKVideoNode causes a black flash before playback begins

查看:268
本文介绍了在开始播放之前,在SKVideoNode中播放视频会导致黑色闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Sprite Kit和SKVideoNode播放短视频.在将视频节点添加到场景中时(即在初始设置之前或之后),播放器将以黑色闪烁一秒钟.

I'm using Sprite Kit and an SKVideoNode to play back a short video. When the video node is added to the scene, be that before of after the initial set up, the player flashes black for a brief second.

我尝试为视频而不是直接使用AVPlayer播放视频,我还使用KVO仅在视频说可以播放时才加载SKVideoNode.

I have tried using an AVPlayer for the video instead of the video directly, also I've used KVO to only load the SKVideoNode when the video says it's ready to play.

无论哪种方式,在我的视频开始播放之前,我都会先出现黑色闪光.

Either way I get a black flash before my video starts to play.

尽管我不知道这样做是否有帮助,但似乎也没有办法将AVPlayerLayer添加到SKScene/SKView.

Also there doesn't seem to be a way to add an AVPlayerLayer to the SKScene/SKView, although I don't know if that would help.

任何有关下一步尝试的建议都是很好的.

Any suggestions on what to try next would be great.

这是我正在使用的代码

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

这是另一段代码,它只播放视频,并在加载的视频和播放的视频之间提供相同的黑闪光.

Here's an alternative piece of code which just plays the video and gives the same black flash between the video being loaded and it playing.

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

推荐答案

我遇到了类似的问题,并按如下方式解决了该问题:

I had a similar problem and solved it like this:

我将视频的第一帧导出为PNG.然后,我在SKVideoNode前面添加了带有该PNG的SKSpriteNode.开始播放视频时,将SKSpriteNodehidden属性设置为YES.

I exported the first frame of the video as PNG. Then I added a SKSpriteNode with that PNG in front of the SKVideoNode. When I start the video, I set the hidden property of the SKSpriteNode to YES.


这是我的代码的简化示例,其中包含相关部分:


Here is an simplified example of my code with the relevant parts:

-(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;             
        }
    }
}


这是我到目前为止找到的最好的解决方案.希望对您有帮助!


This is the best solution I've found so far. I hope that helps you!

这篇关于在开始播放之前,在SKVideoNode中播放视频会导致黑色闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆