Jul 09

Apple made significant changes to the MPMoviePlayerController API:s in iOS 3.2. Before, all you had to do was to initialize the MPMoviePlayerController and call play. Now things are a bit more complicated because videos can be played on a portion of the screen (good for the iPad) and not just in full screen mode.

But if all you want is to play a video in full-screen mode on all versions of the OS, then here’s some code:

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:introVideoFileName ofType:@""]];
MPMoviePlayerController *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];

if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
  // Use the new 3.2 style API
  moviePlayer.controlStyle = MPMovieControlStyleNone;
  moviePlayer.shouldAutoplay = YES;
  [self.view addSubview:moviePlayer.view];
  [moviePlayer setFullscreen:YES animated:YES];
} else {
  // Use the old 2.0 style API
  moviePlayer.movieControlMode = MPMovieControlModeHidden;
  [moviePlayer play];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
  MPMoviePlayerController *moviePlayer = [notification object];
  [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                  name:MPMoviePlayerPlaybackDidFinishNotification 
                                                object:moviePlayer];

  // If the moviePlayer.view was added to the view, it needs to be removed
  if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
    [moviePlayer.view removeFromSuperview];
  }

  [moviePlayer release];
}

 

Recently I’ve been doing some development using cocos2d and I had to make some small changes to the code. (If your app is in portrait mode, then you may not have to do the rotation transform shown below.)

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:introVideoFileName ofType:@""]];
MPMoviePlayerController *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];

if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {
  // Use the new 3.2 style API
  moviePlayer.controlStyle = MPMovieControlStyleNone;
  moviePlayer.shouldAutoplay = YES;
  // This does blows up in cocos2d, so we'll resize manually
  // [moviePlayer setFullscreen:YES animated:YES];
  [moviePlayer.view setTransform:CGAffineTransformMakeRotation((float)M_PI_2)];
  CGSize winSize = [[CCDirector sharedDirector] winSize];
  moviePlayer.view.frame = CGRectMake(0, 0, winSize.height, winSize.width);// width and height are swapped after rotation
  [[[CCDirector sharedDirector] openGLView] addSubview:moviePlayer.view];
} else {
  // Use the old 2.0 style API
  moviePlayer.movieControlMode = MPMovieControlModeHidden;
  [moviePlayer play];
}

written by Nick \\ tags: ,