MPMoviePlayerController

I need implement the following two features in most of my iOS apps, e.g., iClip:

  • auto-play-next-episode if available
  • auto resume from the stop point last time stopped/pasued

By looking into the developers doc, the first one is easy to do, just observe the MPMoviePlayerPlaybackDidFinishNotification:


-(void)playMovieAtURL:(NSURL*)theURL
{

	MPMoviePlayerViewController* mpvc =[[MPMoviePlayerViewController alloc] initWithContentURL:theURL];
	[self presentModalViewController:mpvc animated:NO];

	double stoppedPoint = [[NSUserDefaults standardUserDefaults] doubleForKey:[mpvc.moviePlayer.contentURL absoluteString]];
	if (stoppedPoint > 0.0) {
		mpvc.moviePlayer.initialPlaybackTime = stoppedPoint;
	}

	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(myMovieFinishedCallback:)
												 name:MPMoviePlayerPlaybackDidFinishNotification
											   object:mpvc.moviePlayer];

	if (![self isOS5])
	{
		mpvc.moviePlayer.useApplicationAudioSession = NO;
	}

	self.mpvc = mpvc; //retain it
	[mpvc release];
	return;

}

// When the movie is done,release the controller.
-(void)myMovieFinishedCallback:(NSNotification*)aNotification
{

    if ([[aNotification object] isKindOfClass:[MPMoviePlayerController class]])
    {
        MPMoviePlayerController* theMovie=[aNotification object];
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:MPMoviePlayerPlaybackDidFinishNotification
                                                      object:theMovie];

        [self  dismissModalViewControllerAnimated:NO];

        theMovie = nil;
        [theMovie release];
    }

    NSDictionary *userInfo = [aNotification userInfo];

    int reason = [[userInfo objectForKey:@"MPMoviePlayerPlaybackDidFinishReasonUserInfoKey"] intValue];
    NSLog(@"clip finished because %d. play next one if available.", reason);
    // ios 4 always return MPMovieFinishReasonPlaybackEnded even it's user exit it, a bug. don't provide play next feature for ios4 users then.
    if ([self isOS5] && reason == MPMovieFinishReasonPlaybackEnded)
    {
        [self playNextEpisodeIfAvailable];
    }
}

Notice the finished reason in iOS 4 (bug) is always MPMovieFinishReasonPlaybackEnded, I had to implement the same feature in MPMoviePlayerPlaybackStateDidChangeNotification for iOS 4 users:


-(void)myMovieStateChangedCallback:(NSNotification*)aNotification
{
    if ([[aNotification object] isKindOfClass:[MPMoviePlayerController class]])
    {
        MPMoviePlayerController* theMovie=[aNotification object];
        if (theMovie.playbackState == MPMoviePlaybackStateStopped && theMovie.currentPlaybackTime == theMovie.playableDuration) {
            NSLog(@"clip finished!");

            if ([self isOS5]) {

            }else{
                [self playNextEpisodeIfAvailable];
            }
        }

For second feature, saving stop point when movie exit, I also use the MPMoviePlayerPlaybackStateDidChangeNotification to catch the currentPlaybackTime, I’ve tried catch it in MPMoviePlayerPlaybackDidFinishNotification, the value is zero, too late.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s