0

I am using CCVideoPlayer to play a video in my game but it has a slight delay before it plays which causes a black screen to show before it plays. Is there some way to preload the video or set up CCVideoPlayer in a way that does away with this delay. Here is how I am using it, I have a loading scene upon start up and when all my resources are loaded I tell it to switch to the main menu like so:

[[CCDirector sharedDirector] replaceScene:[MainMenu scene]];

And then this is how I am playing the movie in the main menu:

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];
    MainMenu *layer = [MainMenu node];
    [scene addChild: layer];

    return scene;
}

- (id) init {

    if( (self=[super init])) {

                [CCVideoPlayer setDelegate: self];

    }

    return self;
}

- (void)onEnter{

        [self playVideo];
    }

    [super onEnter];
}

-(void)onExit{

    [super onExit];
}

- (void) playVideo {

    [CCVideoPlayer playMovieWithFile: @"MenuBuild.m4v"];
}

- (void) movieStartsPlaying {

    [[CCDirector sharedDirector] stopAnimation];

}

- (void) moviePlaybackFinished
{

    [[CCDirector sharedDirector] startAnimation];

 }

#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
// Updates orientation of CCVideoPlayer. Called from SharedSources/RootViewController.m
- (void) updateOrientationWithOrientation: (UIDeviceOrientation) newOrientation
{
    [CCVideoPlayer updateOrientationWithOrientation:newOrientation ];
}
#endif

- (void) dealloc {

    [CCVideoPlayer setDelegate: nil];

    [super dealloc];
}
@end

Is there something different that I could do to have the video instantly start playing instead of a slight delay with a black screen?

Stephen
  • 499
  • 9
  • 28
  • the delay is normal, the uimovieplayercontroller has callbacks telling you when it's ready but they may not be implemented by ccvideo... – CodeSmile May 01 '13 at 20:34
  • So how do I get around the delay. Because that doesn't look good. – Stephen May 01 '13 at 20:42
  • search for MPMoviePlayerLoadStateDidChangeNotification … this notification tells you when MPMoviePlayerViewController is ready to play the video, at which point you would change its hidden flag to NO to show it. There's more to that though but I believe there are several examples floating about on the net. – CodeSmile May 02 '13 at 00:48
  • or add the first screen of video as an image to background so no black screen :-) – Hamdullah shah May 02 '13 at 04:42
  • @LearnCocos2D I can't seem to find an example that fits what I am trying to do. I would be very grateful if you could show me some code or if you know of an example that I could look at! – Stephen May 02 '13 at 13:36

1 Answers1

0

What you can do to hide the black flicker is to show an image of the first frame on top of the video. After half a second (or however long the black flicker lasts) hide the first frame so that the video shows. Here is an example:

CCSprite* first_frame = [CCSprite spriteWithFile:@"first_frame.png"];
[self addChild:first_frame];

id delay_action = [CCDelayTime actionWithDuration:0.5f];

id call_action = [CCCallBlock actionWithBlock:^
{
    first_frame.visible = FALSE;
}];

[first_frame runAction:[CCSequence actions:delay_action, call_action, nil]];

I haven't used CCVideoPlayer but if you can't place a sprite over on top of the video, try setting the alpha of the video to be initially at 0, then in the call block set it to visible so that video can be seen playing after the initial half second delay (or whatever amount of time is needed). That would cause the video to appear after the black flicker has passed.

Don't be afraid to modify the video player if you need to add a way to toggle its visibility.

In my apps I start an MPMoviePlayerViewController off as being invisible, then I set it to visible after a short delay to hide that flicker. In my cocos2d apps that use video I add the movie player using [[[CCDirector sharedDirector] view] addSubview:...]; so I don't use the CCVideoPlayer personally but it should still work for you.

After looking at CCVideoPlayer on github, you should be able to set it's movie view to invisible in playMovieAtURL and in the code block example above, set it to visible after the time delay. I hope this helps.

Allen S
  • 1,042
  • 2
  • 9
  • 14