Online Book Reader

Home Category

iPhone Game Development - Chris Craft [47]

By Root 1589 0

#import

#import

@class iFlameViewController;

@interface iFlameAppDelegate : NSObject

{

IBOutlet UIWindow *window;

}

@property (nonatomic, retain) UIWindow *window;

@end

Next, you will need to create the actual MPMoviePlayerController object instance. This will go under IBOutlet declaration line that is located near the middle of the file. After this you will need to add an NSURL object to help keep track of movie URL:

MPMoviePlayerController *mMoviePlayer;

NSURL *mMovieURL;

Note

You can use the MPMoviePlayerController to play audio as well as video. The only drawback is the user will be presented with a rather dull screen, with, at most, basic playback controls, while the audio plays.

You will now need to add a couple of notification callback methods so that the movie player object and the iFlame application can load the movie and handle when the movie is done playing. You should add these items right before the @end statement:

-(NSURL *)movieURL;

-(void)initMoviePlayer;

-(void)moviePlayBackDidFinish: (NSNotification*)notification;

Once you have performed these changes, your iFlameAppDelegate.h file should look like the following:

#import

@class MainViewController;

@interface iFlameAppDelegate : NSObject {

UIWindow *window;

MPMoviePlayerController *mMoviePlayer;

NSURL *mMovieURL;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) MainViewController *mainViewController;

-(NSURL *)movieURL;

-(void)initMoviePlayer;

-(void)moviePlayBackDidFinish: (NSNotification*)notification;

@end

If you look back in the iFlameAppDelegate.h file, you will see that the iFlameAppDelegate implements the UIApplicationDelegate, which, according to the API Reference, means there is an optional applicationDidFinishLaunching event that is called when the application has finished launching. You will use this event to start your video.

Now you will move on to updating the corresponding iFlameAppDelegate.m file. Start by updating the applicationDidFinishLaunching method. Make the changes required so that your applicationDidFinishLaunching method matches the following:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

[self initMoviePlayer];

[window makeKeyAndVisible];

}

Tip

The media player always does a fade to black whenever a video finishes playing. To avoid this you can copy and paste your video multiple times, even hundreds of times, in QuickTime Pro; while the play time will increase, the file size will not.

Now when the iFlame application finishes loading, it will call your applicationDidFinishLaunching method, which is set to then call the initMoviePlayer method. You will now add the following initMoviePlayer method to your code by modifying the current initMoviePlayer method to match this one:

-(void)initMoviePlayer

{

mMoviePlayer = [[MPMoviePlayerController alloc]

initWithContentURL:[self movieURL]];

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(moviePlayBackDidFinish:)

name:MPMoviePlayerPlaybackDidFinishNotification

object:mMoviePlayer];

mMoviePlayer.scalingMode = MPMovieScalingModeAspectFill;

mMoviePlayer.movieControlMode = MPMovieControlModeHidden;

mMoviePlayer.backgroundColor = [UIColor blackColor];

[mMoviePlayer play];

}

There's a lot going on in this method, so take a moment to review it now.

The following line of code creates a new MPMoviePlayerController object and assigns it to our mMoviePlayer object. It also initializes the newly created MPMoviePlayerController object with the movie stored at the movieURL location:

mMoviePlayer = [[MPMoviePlayerController alloc]

initWithContentURL:[self movieURL]];

The purpose of the following line of code is to tell the MPMoviePlayerController object that we want to be notified when the movie finishes playback. The name of this notification is MPMoviePlayerPlaybackDidFinishNotification,

Return Main Page Previous Page Next Page

®Online Book Reader