14.1. Movie Player Controllers

The MPMoviePlayerController class is initialized with an NSURL object, which you've already learned about. You can instantiate the NSURL class to refer to a local file or a remote website URL. To initialize the movie player controller, use the class's initWithContentURL method and provide an NSURL object. An example follows:

MPMoviePlayerController *moviePlayer = [ [ MPMoviePlayerController alloc ]
    initWithContentURL: [ NSURL URLWithString: @"http:// ..." ] ];

To initialize a movie player to play a local file, use the NSURL class's fileURLWithPath method:

NSString *path = [ NSString stringWithFormat: @"%@/Documents/movie.m4a",
    NSHomeDirectory()
];

MPMoviePlayerController *moviePlayer = [ [ MPMoviePlayerController alloc ]
    initWithContentURL: [ NSURL fileURLWithPath: path ] ];

14.1.1. Properties

Once you have created the movie player controller, you can set a few properties.

14.1.1.1. Controls

You can specify the configuration of the movie's controls by setting the movieControlMode property:

moviePlayer.movieControlMode = MPMovieControlModeDefault;

You may use the following values to define this property.

Mode Description
MPMovieControlModeDefault Displays play/pause, volume, and timeline
MPMovieControlModeVolumeOnly Displays only the volume control
MPMovieControlModeHidden No controls

14.1.1.2. Aspect ratio

You may also adjust the movie's aspect ratio by setting the scalingMode property:

moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

You may use the following aspect ratio values.

Value Description
MPMovieScalingModeNone No scaling applied
MPMovieScalingModeAspectFit Fit to screen uniformly
MPMovieScalingModeAspectFill Fill entire screen uniformly; may clip
MPMovieScalingModeFill Fill entire screen; do not maintain aspect

14.1.1.3. Background color

The background color is used when the movie player is transitioning to and from playback, and is also used to fill the empty space when the movie does not fill the entire screen. The default background color is black, but you may change this by setting the backgroundColor property to a UIColor object:

moviePlayer.backgroundColor = [ UIColor whiteColor ];

14.1.2. Starting and Stopping the Movie

To play the movie, invoke the movie player's play method:

[ moviePlayer play ];

The movie player controller will automatically transition your current view to the movie player and begin playing the movie.

The movie will stop when the user taps the Done button or when the controller's stop method is invoked:

[ moviePlayer stop ];

When the movie stops, the player automatically transitions back to the previous view your application was displaying.

14.1.3. Notifications

Your application can configure notifications to be sent when the movie player finishes loading content, finishes playing, or when the user changes its aspect ratio. The movie player posts events to Cocoa's notification center, which you can configure to relay those events to an object in your application. To receive a notification, use the NSNotificationCenter class to add an observer to the movie player:

NSNotificationCenter *notificationCenter = [ NSNotificationCenter defaultCenter ];
[ notificationCenter addObserver: self
    selector:@selector(moviePlayerPreloadDidFinish:)
    name: MPMoviePlayerContentPreloadDidFinishNotification
    object: moviePlayer
];

                                          

Notifications are sent to your delegate class and the target method you specify. The notification parameter lets you know which event triggered the invocation of the delegate method:

-(void)moviePlayerPreloadDidFinish:(NSNotification*)notification
{
    NSLog(@"All my content are belong to me!\n");
}

You may observe the following notifications:



MPMoviePlayerContentPreloadDidFinishNotification

Posted when the movie player has finished preloading its content. Because content can be played while only partially loaded, this notification may be posted after the content has already begun playing.



MPMoviePlayerScalingModeDidChangeNotification

Posted when the user changes the scaling mode of the movie. The user can tap a scaling icon to switch between full screen and windowed modes.



MPMoviePlayerPlaybackDidFinishNotification

Posted when the movie has finished playing or when the user presses the Done button.

14.1.4. Further Study

Movie players are simple objects, so we'll let you try your hand at them without too much hand-holding.

  • Take any existing example from this book and add an MPMoviePlayerController. You won't need to add any transitions or special code, as the controller will automatically take over the screen when you begin playing.

  • Check out the MPMoviePlayerController.h prototypes in your SDK's header files. You'll find them deep within /Developer/Platforms/iPhoneOS.platform, inside the MediaPlayer framework's Headers directory.

  • Check out the NSNotification.h prototypes in Mac OS X's header files. You'll find them in /System/Library/Frameworks/Foundation.framework/Headers.