iPhone Developer Tips Visitor Stats: 126,691 Pageviews and 94,296 visitors in the past 30 days.
|
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)
Share with iOS Developers:
Comments
3 Responses to “How to Play Movies from the Application Bundle or from a Remote Server (URL)”
Leave Comment
Hi, thank you very much for sharing the source code.
I will definitely try this out tonight.
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