Fork me on GitHub

Playing Video on iPhone Cocos2D-X

原文地址:http:///www.cocos2d-x.org/boards/7/topics/550

Got this working, thought I'd share:

Can also be found on my not very often updated blog here

OK, first thing you need to know is that Cocos2d doesn’t play videos, it leaves this up to the iOS SDK on the devices. The following code therefore is Obj C which can be inserted into your projects AppController.mm file or you could create a new mm interface file and put it in there.

Add the “AVFoundation.framework” to your projects Frameworks (located the Frameworks folder, right click, select “Add -> Existing Frameworks”).
Add the “MediaPlayer.framework” to your projects Frameworks.
You will need a video to play and to add it to the resources in the project. Use your own or download my sample video from here
Add the following code to your AppController.mm
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

MPMoviePlayerViewController *playerViewController=NULL;
int g_iPlayVideoState=0;

- (void)PlayVideo:(int)iStateAfterPlay fullscreen:(int)iFullScreen file:(NSString*)strFilennameNoExtension fileExtension:(NSString*)strExtension
{
    NSLog(@"PlayVideo start");

    g_iPlayVideoState = 2;

    NSString *url = [[NSBundle mainBundle] pathForResource:strFilennameNoExtension ofType:strExtension];

    CGRect rScreen;
    rScreen.origin.x = 0;
    rScreen.origin.y = 0;
    rScreen.size.width = 480;
    rScreen.size.height = 320;
    //rScreen = CGRect::CGRectMake(0,0, 480, 320);        

    if( iFullScreen==0 )
    {
        MPMoviePlayerController *player2 = [[MPMoviePlayerController alloc] 
                                            initWithContentURL:[NSURL fileURLWithPath:url]];

        [[NSNotificationCenter defaultCenter] 
            addObserver:self
            selector:@selector(movieFinishedCallback:)
            name:MPMoviePlayerPlaybackDidFinishNotification
            object:player2];

        //---play partial screen---
        player2.view.frame = rScreen;
        [self addSubview:player2.view];
        player2.shouldAutoplay=TRUE;
        //---play movie---
        [player2 play];
    }   
    else
    {
        playerViewController = [[MPMoviePlayerViewController alloc] 
                                initWithContentURL:[NSURL fileURLWithPath:url]];

        [[NSNotificationCenter defaultCenter] 
        addObserver:self
        selector:@selector(movieFinishedCallback:)
        name:MPMoviePlayerPlaybackDidFinishNotification
        object:[playerViewController moviePlayer]];

        playerViewController.view.frame = rScreen;

        cocos2d::CCDirector::sharedDirector()->purgeCachedData();

        // Add the view
        EAGLView *view = [EAGLView sharedEGLView];
        [view addSubview:playerViewController.view];

        [view sendSubviewToBack:view];

        NSLog(@"pView inserted");

        // Add the view - Use these three lines for Cocos 2D X
        window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
        [window addSubview: playerViewController.view];
        [window makeKeyAndVisible];

        //---play movie---
        MPMoviePlayerController *player = [playerViewController moviePlayer];
        player.scalingMode=MPMovieScalingModeAspectFit;
        player.shouldAutoplay=TRUE;
        [player play];
    }

    g_iPlayVideoState = 1;

    NSLog(@"PlayVideo done");
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void) movieFinishedCallback:(NSNotification*) aNotification 
{
    NSLog(@"movieFinishedCallback");
    MPMoviePlayerController *player = [aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];    

    [player.view removeFromSuperview];
     [player autorelease];    
    g_iPlayVideoState = 0;

    NSLog(@"movieFinishedCallback done");
}
    
Call the PlayVideo function from with a line like the following. Call it from your AppController’s didFinishLaunchingWithOptions function just before (or after) the app.run();

[self PlayVideo:0 fullscreen:1 file:@"ThreeSheets_Title_480x272" fileExtension:@"m4v"];
In Appelegate.cpp, find your applicationDidFinishLaunching() function and after “pDirector->setOpenGLView(m_pMainWnd);” add the line:

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
The above gl command sets your Cocos2D-X window to transparent wherever it is black, so you can see through it to the video behind.

I have a global variable “g_iPlayVideoState” which you can monitor in your Cocos2D applications code to detect when the video ends, its values are 2=initializing, 1=playing, 0=finished. At the end of play you might want to put the views back to non-transparent with the call “glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

posted on 2012-05-22 11:19  pengyingh  阅读(1235)  评论(0编辑  收藏  举报

导航