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]; }
July 13th, 2010 at 11:46
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.
July 16th, 2010 at 10:15
Finally, I succeeded to play a movie on iPod touch with this code (the first one). Thank you so much.
November 29th, 2011 at 10:41
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?
April 18th, 2012 at 00:02
@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.
September 12th, 2012 at 13:49
For sake of completeness: Use a webview instead of MPMoviePlayer for youtube videos.