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: ,

5 Responses to “Play Video with MPMoviePlayerController in iOS 3.0 and 3.2/4.0”

  1. thearchiteck Says:

    Thanks for posting this, I was having a very hard time trying to figure out how to use MPMoviePlayerController with video on the iPad.

    Apple has very poor documentation for usage of the media player framework for iPad.

  2. MasahiroFurukawa Says:

    Finally, I succeeded to play a movie on iPod touch with this code (the first one). Thank you so much.

  3. Yos Says:

    I’ve tried the code published here but I changed the first line to:

    NSURL *url = [NSURL URLWithString:@”http://www.youtube.com/watch?v=L9szn1QQfas&fs=0″];

    That doesn’t work for me, It just shows a black screen for a second and gets back to my application view.
    What am I doing wrong?

  4. Veera Says:

    @YOS

    This player will only play the video files which are in a playlist file with the format .m3u8.

    To play youtube files you have to integrate youtube api in to your application.

  5. Jason Says:

    For sake of completeness: Use a webview instead of MPMoviePlayer for youtube videos.

Leave a Reply