cocos2d: playing a video in the background of a menu

前端 未结 1 905
-上瘾入骨i
-上瘾入骨i 2021-01-30 03:17

I have a a CCLayer with the start menu of my app and I would like to have a short movie playing in the background. I have succeded in playing a movie in the glView but when it p

相关标签:
1条回答
  • 2021-01-30 04:20

    After spending some time with this I found the answer. (I want to do this too, but later, so I may as well get it going now!)

    I used the code and techniques in this link by ascorbin, with help from this link. It is not complete unless you also add the following line to the EAGLView:

    glView.opaque = NO;
    

    The movie view is along side of the EAGLView, and pushed to the back to get what you are looking for. You may also have to tweak the transparency of the EAGLView like it mentions in that post. Hope that helps.


    The things I needed to modify are listed here.

    You need to add apple's mediaPlayer.framework to your project and include the following line in your app delegate .h:

    #import <MediaPlayer/MediaPlayer.h>
    

    In CCDirector's setGLDefaultValues method, set glClearColor's alpha to be 0.0f not 1.0f:

    //  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    

    When creating your EAGLView in applicationDidFinishLaunching: change the following things:

    Change the pixelFormat from the old value to be this:

    // kEAGLColorFormatRGB565
    kEAGLColorFormatRGBA8
    

    Make sure to add the EAGLView as a sub-view (and make it non-opaque) rather than as the main view:

    //  [viewController setView:glView];
        [viewController.view addSubview:glView];
        glView.opaque = NO;
    

    Finally, before running your scene you can add your code to play the looping movie. I added a method to the app delegate for this. You could also add a method to stop your movie from playing here in the app delegate as well for when (if?) you need to remove your movie.

    [self startMovie];
    

    and the method itself:

    - (void) startMovie
    {
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"m4v"]];
        moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    
        if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
            // Use the new 3.2 style API
            moviePlayer.controlStyle = MPMovieControlStyleNone;
            moviePlayer.shouldAutoplay = YES;
            moviePlayer.repeatMode = MPMovieRepeatModeOne;
            CGRect win = [[UIScreen mainScreen] bounds];
    
            moviePlayer.view.frame = CGRectMake(0, 0, win.size.height, win.size.width);
            [viewController.view  addSubview:moviePlayer.view];
            [viewController.view  sendSubviewToBack:moviePlayer.view];
        } 
        else
        {
            // Use the old 2.0 style API
            moviePlayer.movieControlMode = MPMovieControlModeHidden;
            [moviePlayer play];
        }
    }
    

    Note that you do not have to add the observer like is listed in the linked example, nor the function to repeat the movie, this is done automatically with the 'MPMovieRepeatModeOne' option. If you don't want the movie to repeat you can choose the 'MPMovieRepeatModeNone' option here as well.

    I should also note that I was unable to get this to work with the old pre-3.2 API (so in your 3.1.3 case it was not working), the video didn't show through the EAGLView. I am not sure what is happening there, but I have seen no specific options on what to do about that.

    0 讨论(0)
提交回复
热议问题