The cutscene is a concept that has existed since the early days of video games. Cutscenes are usually interspersed in between gameplay segments or shown when a game is loading. For more complex cutscenes it is often advantageous to use full motion video. In this recipe we will see how to insert a video into our game.
This recipe requires the extra step of linking the MediaPlayer iOS framework to our project:
Right-click your project under Groups & Files.
Click Add | Existing Frameworks
Under iOS SDK select MediaPlayer.framework
Keep in mind that RecipeCollection01
already has this library linked.
Now, execute the following code:
#import <MediaPlayer/MediaPlayer.h> @interface Ch1_PlayingVideoFiles { MPMoviePlayerController *moviePlayer; } @implementation Ch1_PlayingVideoFiles -(CCLayer*) runRecipe { //Load our video file NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"example_vid" ofType:@"mov"]]; //Create a MPMoviePlayerController object 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]; //Set the movie's control style and whether or not it should automatically play. if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) { //Use the new 3.2 style API. moviePlayer.controlStyle = MPMovieControlStyleNone; moviePlayer.shouldAutoplay = YES; CGSize winSize = [[CCDirector sharedDirector] winSize]; moviePlayer.view.frame = CGRectMake(45, 50, winSize.width-90, winSize.height-100); [[[CCDirector sharedDirector] openGLView] addSubview:moviePlayer.view]; } else { //Use the old 2.0 style API. moviePlayer.movieControlMode = MPMovieControlModeHidden; [self playMovie]; } return self; } -(void)moviePlayBackDidFinish:(NSNotification*)notification { //If playback is finished we stop the movie. [self stopMovie]; } -(void)playMovie { //We do not play the movie if it is already playing. MPMoviePlaybackState state = moviePlayer.playbackState; if(state == MPMoviePlaybackStatePlaying) { NSLog(@"Movie is already playing."); return; } [moviePlayer play]; } -(void)stopMovie { //We do not stop the movie if it is already stopped. MPMoviePlaybackState state = moviePlayer.playbackState; if(state == MPMoviePlaybackStateStopped) { NSLog(@"Movie is already stopped."); return; } //Since playback has finished we remove the observer. [[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]; } } -(void)cleanRecipe { [super cleanRecipe]; [self stopMovie]; [moviePlayer release]; } @end
This recipe shows us how to load, play, and stop a movie.
Using MPMoviePlayerController:
This recipe is merely the tip of the iceberg regarding movie playback. Movies can also be played back in fullscreen mode, in portrait mode, and using a variety of other options. Please refer to official Apple documentation when customizing and/or adding to this technique.
The observer pattern is not used very often while doing Cocos2d programming, but it is a powerful mechanism and it is the recommended way of knowing when your video has finished playback. You can read more about observers by referring to the official
Objective-C
documentation.According to Apple documentation it is recommended that you compress your movies using
H.264/MPEG-4
for video,AAC
audio and one of the following file formats:MOV
,MP4
,MPV
,3GP
.It is also recommended that your movies be no larger than
640x480
and run no faster than30 FPS
.The movie used in the recipe was created and encoded using Apple's iMovie software.
For more information please consult official Apple iOS SDK documentation.