Displaying a video in iOS

The best way I have found to display videos in iOS using Xcode is using a MPMoviePlayerViewController.

First off, in your .h (header) file add the following:

MPMoviePlayerViewController *myMovie;

Once you have done that, you can now call and create it in the main file (.m). There are various ways to instigate it, but the one I have found to be the best is initWithContentURL which allows you to specify the URL to the video on creation, and by doing this also check if the file exists, for example, see the following code:

-(void)loadVideo:(NSString *)video {
    NSString *videoString = [[NSBundle mainBundle] pathForResource:[video stringByDeletingPathExtension] ofType:[video pathExtension]];
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:videoString];
    if(fileExists) {
        NSURL *mainVideo = [NSURL fileURLWithPath:videoString];
        myMovie = [[MPMoviePlayerViewController alloc] initWithContentURL:mainVideo];
        [myMovie.view setAlpha:0];
        [myMovie.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(movieFinishedCallback:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:myMovie.moviePlayer];
        [UIView animateWithDuration:0.4
                              delay:0.0
                            options: UIViewAnimationCurveEaseInOut
                         animations:^{ [myMovie.view setAlpha:1];}
                         completion:^(BOOL finished){ }];
    }
    else {
        NSLog(@"Unable to load %@",video);
    }
}

Where video is a string, such as MyVideo.mp4, this would then show the video, with a 0.4 second fade in (as it will just display nicer to the user)

Sometimes, the aspect ratio of the video doesn't fill the screen, in this case it is a good idea to add the following line:

[realMovie.moviePlayer setScalingMode:MPMovieScalingModeAspectFill];

By doing this, the video will now attempt to fill the screen.
You may have noticed that we have a NSNotificationCenter in there; this is so that we get notification of when the video finishes, this calls a custom method called movieFinishedCallback: the code for which you can see below:

-(void)movieFinishedCallback:(id)selector {
    [UIView animateWithDuration:0.4
                          delay:0.0
                        options: UIViewAnimationCurveEaseInOut
                     animations:^{ [realMovie.view setAlpha:0];}
                     completion:^(BOOL finished){
                         [realMovie removeFromParentViewController];
                     }];

In the above code, when the video finishes, it fades it out (in 0.4 of a second) then removes it from the parentView, ready to be added again, in the first batch of code.

Settings

Should you want to not have the controls displayed, just update the setControlStyle to MPMovieControlStyleNone.

In some cases, where you don't have the status bar displaying, you may notice it reappears after the video finishes. to solve this, add the following line to movieFinishedCallback:

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:TRUE];

I hope this helps someone!