본문 바로가기

Program/iOS

iOS mpmovieplayercontroller

** 개인적 메모입니다. 태클 사절 **

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:

  1. NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:introVideoFileName ofType:@""]];  
  2. MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];  
  3.   
  4. // Register to receive a notification when the movie has finished playing.  
  5. [[NSNotificationCenter defaultCenter] addObserver:self  
  6.                                          selector:@selector(moviePlayBackDidFinish:)  
  7.                                              name:MPMoviePlayerPlaybackDidFinishNotification  
  8.                                            object:moviePlayer];  
  9.   
  10. if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {  
  11.   // Use the new 3.2 style API  
  12.   moviePlayer.controlStyle = MPMovieControlStyleNone;  
  13.   moviePlayer.shouldAutoplay = YES;  
  14.   [self.view addSubview:moviePlayer.view];  
  15.   [moviePlayer setFullscreen:YES animated:YES];  
  16. else {  
  17.   // Use the old 2.0 style API  
  18.   moviePlayer.movieControlMode = MPMovieControlModeHidden;  
  19.   [moviePlayer play];  
  20. }  
  1. - (void) moviePlayBackDidFinish:(NSNotification*)notification {  
  2.   MPMoviePlayerController *moviePlayer = [notification object];  
  3.   [[NSNotificationCenter defaultCenter] removeObserver:self  
  4.                                                   name:MPMoviePlayerPlaybackDidFinishNotification  
  5.                                                 object:moviePlayer];  
  6.   
  7.   // If the moviePlayer.view was added to the view, it needs to be removed  
  8.   if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {  
  9.     [moviePlayer.view removeFromSuperview];  
  10.   }  
  11.   
  12.   [moviePlayer release];  
  13. }  

 

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.)

  1. NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:introVideoFileName ofType:@""]];  
  2. MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];  
  3.   
  4. // Register to receive a notification when the movie has finished playing.  
  5. [[NSNotificationCenter defaultCenter] addObserver:self  
  6.                                          selector:@selector(moviePlayBackDidFinish:)  
  7.                                              name:MPMoviePlayerPlaybackDidFinishNotification  
  8.                                            object:moviePlayer];  
  9.   
  10. if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)]) {  
  11.   // Use the new 3.2 style API  
  12.   moviePlayer.controlStyle = MPMovieControlStyleNone;  
  13.   moviePlayer.shouldAutoplay = YES;  
  14.   // This does blows up in cocos2d, so we'll resize manually  
  15.   // [moviePlayer setFullscreen:YES animated:YES];  
  16.   [moviePlayer.view setTransform:CGAffineTransformMakeRotation((float)M_PI_2)];  
  17.   CGSize winSize = [[CCDirector sharedDirector] winSize];  
  18.   moviePlayer.view.frame = CGRectMake(0, 0, winSize.height, winSize.width);// width and height are swapped after rotation  
  19.   [[[CCDirector sharedDirector] openGLView] addSubview:moviePlayer.view];  
  20. else {  
  21.   // Use the old 2.0 style API  
  22.   moviePlayer.movieControlMode = MPMovieControlModeHidden;  
  23.   [moviePlayer play];  
  24. }