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

Tue, Jul 20

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- (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:@"http://someurlsomewhere.com/movie.mp4"];
  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)

8 comments

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

I will definitely try this out tonight.

by Wasif Malik on Jul 21, 2010. #

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

by Forrest on Jul 22, 2010. #

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

by John Muchow on Jul 26, 2010. #

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];

by Philip Borges on Mar 26, 2011. #

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?

Thanks,

Bill

by Bill on May 12, 2011. #

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];

by John Muchow on May 12, 2011. #

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,

Bill

by Bill on May 12, 2011. #

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

by Dave on May 16, 2011. #