How to Play Movies from the Application Bundle or from a Remote Server (URL)

In the previous two posts I wrote about how to create a movie player that would work across OS versions, as well as how to play a movie in portrait mode (on 3.2 and above) : Display iPhone Movies in Portrait Mode and Getting MPMoviePlayerController to Cooperate with iOS4, 3.2 (iPad) and Earlier Versions of iPhone SDK

For both posts, in order to create a working example that one could download and run as is, the code examples referenced a movie that I included in the application bundle. Although handy, this prompted a number of questions about how to play a movie from a remote resource.

I’ve updated the original examples for both posts, with an option to play movies from the application bundle or from a remote server.

Play Movie from Server

The first change I made was in CustomMoviePlayerViewController.m, where I added a method to initialize the movie player with a NSURL object:

- (id)initWithURL:(NSURL *)URL
  if (self = [super init])
    movieURL = URL;
    [movieURL retain];
  return self;
- (id)initWithPath:(NSString *)moviePath
  if (self = [super init])
    movieURL = [NSURL fileURLWithPath:moviePath];    
    [movieURL retain];
  return self;

For both methods, the end result is an initialized NSURL object.

The other code change is in TestViewController.m where I updated the loadMoviePlayer method to support calling either of the above methods:

- (void)loadMoviePlayer
  // Play movie from app bundle
  NSString *path = [[NSBundle mainBundle] pathForResource:@"Movie-1" ofType:@"mp4" inDirectory:nil];
  moviePlayer = [[[CustomMoviePlayerViewController alloc] initWithPath:path] autorelease];
  // Play movie from URL
  NSURL *movieURL = [NSURL URLWithString:@""];
  moviePlayer = [[[CustomMoviePlayerViewController alloc] initWithURL:movieURL] autorelease];
  // Show the movie player as modal
  [self presentModalViewController:moviePlayer animated:YES];
  // Prep and play the movie
  [moviePlayer readyPlayer];    

Comment in/out the relevant code in lines 4 – 11 to swap between playing from a file in the bundle and a movie accessible on a remote server.

Download Movie Player Source Code

MoviePlayer in Landscape Mode

MoviePlayer in Portrait Mode (requires 3.2 OS and greater)

  1. Hi, thank you very much for sharing the source code.

    I will definitely try this out tonight.

  2. This can play online remote video ? Just several questions:
    1. What is the protocol ?
    2. Who manages downloading ?
    3. What are supported video formats ?

    • All the details are managed by the OS. All you need to provide is a URL to a movie in a format supported by the player. Common formats are .mov, .mp4, .mpv, and .3gp

  3. Hi,

    Thank you for posting this. The sample code on playing movies from Apple’s iOS Reference Library doesn’t work so I was glad that I found your site. I have a question though:

    I’ve built an RSS reader that plays video podcasts. It works fine when connected to WIFI network but stalls every 7 seconds to load when on 3G network. Therefore Apple rejected the app because it didn’t comply with HTTP live streaming protocol. Now I’ve added the MediaPlayer framework and your code but I’m having troubles loading the vidoes.

    Any ideas?

    My Code:
    // Play movie from URL
    NSURL *movieURL = [NSURL URLWithString:[item objectForKey:@”podcastLink”]];

    mp = [[[MoviePlayerViewController alloc] initWithURL:movieURL] autorelease];

    // Show the movie player as modal
    [self presentModalViewController:mp animated:YES];

    // Prep and play the movie
    [mp readyPlayer];

  4. Thanks very much for posting this. I would like to make the video start as soon as the app launches but I have been unsuccessful so far.

    I tried calling [self loadMoviePlayer]; from within the TestViewController’s viewDidLoad() but nothing happens. Am I missing something?



    • It may be a timing issue with the views. In place of [self loadMoviePlayer] you could try tinkering with something like this:

      [self performSelector:@selector(loadMoviePlayer) withObject:nil afterDelay:1];

      • Thanks so much for the immediate reply, John. Your technique worked great. Looks like I got it down to a very small delay as well…

        – (void) viewDidLoad
        [super viewDidLoad];
        [self performSelector:@selector(loadMoviePlayer) withObject:nil afterDelay:0.01];

        seems to work fine.

        Thanks again,


  5. Thanks dude! Your docs are better than Apple’s!!! :)

Comments are closed.