Tuesday, January 11, 2011

Playing video on iPhone SDK 3.2 and above

I was trying to figure out for a while what is the best way to play a video inside an iPhone application. I wanted the video to play full screen and show the previous view once it is done. After many tries, this is the code that worked best for me:

NSURL *videoUrl = [NSURL URLWithString:url]; 
MPMoviePlayerViewController *mp = 
    [[MPMoviePlayerViewController alloc] initWithContentURL:videoUrl];

if (mp)
{
 mp.moviePlayer.scalingMode = MPMovieScalingModeAspectFit; 
 mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
 [mp.moviePlayer play];
 
 [self presentMoviePlayerViewControllerAnimated:mp];
 
 [mp release];
}