iPhone Game Development - Chris Craft [49]
mMoviePlayer.backgroundColor = [UIColor blackColor];
Note
The movie player's background color is also the color that the screen will fade to and from whenever the video begins or ends. By default, the movie background color is black.
Finally, the simple play command is passed to the movie player object and the magic begins. You can also call stop at any point to pause the video playback and re-call play to resume playback.
Next, you should add the moviePlayBackDidFinish notification method. Since you are using a seamless video loop, whenever the video finishes playback you will simply play it again. Here is the code to make that happen:
-(void)moviePlayBackDidFinish: (NSNotification*)notification
{
MPMoviePlayerController* theMovie=[notification object];
[theMovie play];
}
The notification object that is passed to the moviePlayBackDidFinish method is the MPMoviePlayerController object we assigned to be notified on finishing movie playback. You just need to convert it back to an MPMoviePlayerController object and call the object's play method.
We now have most of the plumbing we need for our iFlame project. One thing that is missing is a way for the application to determine what video it should play. Add the following code, which does that, and then review it yourself before proceeding:
// return a URL for the movie file in our bundle
-(NSURL *)movieURL
{
if (mMovieURL == nil)
{
NSBundle *bundle = [NSBundle mainBundle];
if (bundle)
{
NSString *moviePath = [bundle pathForResource:@”movie” ofType:@”m4v”];
if (moviePath)
{
mMovieURL = [NSURL fileURLWithPath:moviePath];
[mMovieURL retain];
}
}
}
return mMovieURL;
}
First, you check to see if mMovieURL has already been set. If it is nil, this is the first time this method has been called and you will need to assign a value to mMovieURL. Next, check that the iFlame project has an NSBundle. It will as long as you remember to add a video to the iFlame project. The NSBundle object is a powerful class that helps developers to not rely on hard coded paths. This class will find your application's location on the device file system so you can easily access files and resources.
Use the pathForResource method to obtain the movie.M4V file's path. If the path exists, assign it to the mMovieURL string. The last thing is to simply return it back to the calling method.
Caution
Always be sure to test that variables are valid before calling any related methods. A simple if variable != nil is sometimes all it takes to avoid a crashing application.
When the application is done running, it will call dealloc, so be sure to clean up any variables you created there. Since you allocated the memory for the mMoviePlayer and mMovieURL objects, you will need to make sure you deallocate the memory before the application is done. Here is the basic code that is required to do that:
- (void)dealloc
{
[mMoviePlayer release];
[mMovieURL release];
[window release];
[super dealloc];
}
There is one important task remaining before you can try out this application. You have to add the movie.mv4 to the project's resources. From Xcode, find the Resources folder under the iFlame project in the Groups & Files panel. Right-click on the folder and choose Add⇒Existing Items.
Locate the video you wish to use and select it, then click the Add button. You can use one we have provided in this chapter's sample code, or use one of your own. In the next dialog box, be sure to check the Copy items into the destination group's folder check box. Confirm that Reference Type is set to Default and Text Encoding is set to Unicode (UTF-8). Then click the Add button.
Be sure to set Xcode to Simulator | Distribution and then click the Build and Go button. The application should now load up in the Simulator and run. You should see the iFlame application as shown in Figure 4.7.