Playing Video from a URL in iOS7

Here are a few quick notes about how to play video from a URL in iOS7. We will discuss using MPMoviePlayerController for embedded video and MPMoviePlayerViewController for standalone full screen playback. These two classes have very similar names. So, please be careful about that.

Add Library to Project

Both of these classes are available from the MediaPlayer framework. Please add it to your project.

Import the MediaPlayer.h header file as follows:

#import <MediaPlayer/MediaPlayer.h>

Play Embedded Video

In this mode, the video player shares screen space with other views like labels and buttons. First add a property to your view controller for a MPMoviePlayerController object.

@property (nonatomic,strong) MPMoviePlayerController* mc;

Then play a video as follows:

- (void) playMovie {
    NSURL *url = [NSURL URLWithString: 
    MPMoviePlayerController *controller = [[MPMoviePlayerController alloc] 
        initWithContentURL:url]; = controller; //Super important
    controller.view.frame = self.view.bounds; //Set the size

    [self.view addSubview:controller.view]; //Show the view
    [controller play]; //Start playing

You must hold a reference to the MPMoviePlayerController object from the controller. Else, ARC will release the object and things wont work out well.

Show Fullscreen Video

To show a video in a standalone fullscreen mode, we will use MPMoviePlayerViewController. This is a proper UIViewController. Use it as follows:

- (void) playMovie {
    NSURL *url = [NSURL URLWithString:
    MPMoviePlayerViewController *c = [[MPMoviePlayerViewController alloc]

    [self.navigationController presentMoviePlayerViewControllerAnimated:c];

Most applications will need to display a video in landscape mode. MPMoviePlayerViewController does not support landscape mode directly. You will need to take the usual steps to enable landscape mode. First, add support for landscape for the target.


Then, create a class that extends MPMoviePlayerViewController and adds this method:

- (NSUInteger)supportedInterfaceOrientations
    return UIInterfaceOrientationMaskPortrait | 
        UIInterfaceOrientationMaskLandscapeLeft | 

Use the custom class in place of MPMoviePlayerViewController.

5 thoughts on “Playing Video from a URL in iOS7

    • It’s not working for IOS 7 showing below error:
      _itemFailedToPlayToEnd: {
      kind = 1;
      new = 2;
      old = 0;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s