NSologistic
NSologistic

Reputation: 745

Cocos2d .mp4 video not playing

I am trying to play an .mp4 video in cocos2d. Please see my code below. The video doesn't play, only a black background covering a little more than a third of the screen appears.

Instructions.m:

#import "Instructions.h"

@implementation Instructions

- (id)init
{
    self = [super init];
    if (self != nil)
    {
        [self playInstructionsVideo];
    }
    return self;
}

- (void)playInstructionsVideo
{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Instructions" ofType:@"mp4"]];
    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

    // Register to receive a notification when the movie has finished playing.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayer];

    if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)])
    {
        // Use the new 3.2 style API
        moviePlayer.controlStyle = MPMovieControlStyleNone;
        moviePlayer.shouldAutoplay = YES;
        // This does blows up in cocos2d, so we'll resize manually
        // [moviePlayer setFullscreen:YES animated:YES];
        [moviePlayer.view setTransform:CGAffineTransformMakeRotation((float)M_PI_2)];
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        moviePlayer.view.frame = CGRectMake(0, 0, winSize.height, winSize.width);    // width and height are swapped after rotation
        [[[CCDirector sharedDirector] view] addSubview:moviePlayer.view];
    }
    else
    {
        // Use the old 2.0 style API
        moviePlayer.controlStyle = MPMovieControlStyleNone;
        [moviePlayer play];
    }
}

- (void)moviePlayBackDidFinish:(NSNotification *)notification
{
    MPMoviePlayerController *moviePlayer = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayer];

    // If the moviePlayer.view was added to the openGL view, it needs to be removed
    if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)])
    {
        [moviePlayer.view removeFromSuperview];
    }

    [moviePlayer release];
}

@end

Please help me, I am not sure what is causing the abnormal behaviour.

Upvotes: 1

Views: 961

Answers (1)

oopology
oopology

Reputation: 1072

I have a feeling you just copied this code. Anyway from the screenshot it is clear that the orientation change is the result of the setTransform. Removing the line

[moviePlayer.view setTransform:CGAffineTransformMakeRotation((float)M_PI_2)];

should solve the orientation issue.

Upvotes: 2

Related Questions