2013-09-27 119 views
1

我想播放cocos2d中的.mp4视频。请参阅下面的代码。该视频不播放,只出现覆盖略多于三分之一屏幕的黑色背景。Cocos2d .mp4视频不能播放

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 

请帮助我,我不知道是什么原因造成的异常行为。

+0

在设置电影帧大小之前,winSize.height和winSize.width是什么? –

+0

@MichaelDautermann:对不起,我没有看到你的评论。我已经能够播放视频,但方向正面对屏幕的左半部分([截图](http://imgur.com/4IekvPa))。我放了一个CCLOG来检查winSize.width和winSize.height,宽度和高度分别为568.0和320.0。我该如何解决这个问题? – NSologistic

回答

2

我有一种感觉,你只是复制这段代码。无论如何,从截图中可以明显看出,方向改变是setTransform的结果。删除线

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

应解决方向问题。