Pros and cons of MPMoviePlayerController versus launching UIWebView to stream movie

19.5k views Asked by At

I have a client who has video content for the web in Flash format. My task is to help them show the videos in an iPhone app.

I realize that step one is to get these videos into the appropriate Quicktime format for the iPhone.

Then I'm going to have to help the client figure out how or where to host these files. If that's tricky I assume they can be hosted at YouTube.

My chief concern, though, is which approach to take to stream the video. What are the pros and cons of MPMoviePlayerController versus launching UIWebView with the URL of the stream? Is there any difference? Is one of them more or less forgiving? Is one of them a better user experience? Any gotchas I might expect to run into?

I'm assuming playing video is pretty easy on the iPhone. Is it reasonable to try both and have one available as a fallback, or would that be a waste of time? I'm trying to schedule this out a bit, so I'd love to hear real-world experiences from anyone who's done this.

2

There are 2 answers

2
Dan Lorenc On BEST ANSWER

UIWebView cannot actually play videos. Navigating to a Youtube page with a UIWebview will simply launch the iPhone's Youtube App. Doing this a certain way will return control to your app after the video is played. See here: http://iphoneincubator.com/blog/tag/uiwebview

I would recommend using MPMoviePlayer Controller, as long as you are only doing simple streaming. Here's some sample code to get you started:

NSString *url = @"http://www.example.com/path/to/movie.mp4";
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]
                                        initWithContentURL:[NSURL URLWithString:url]];
[moviePlayer play];
11
cduhn On

EDIT: My original example in this answer initialized the webview with a frame of CGRectZero. This worked up to iOS 3.2. Starting with iOS 4 the webview must have a nonzero frame or the video won't play. I've edited my example below to reflect this change.


The accepted answer here isn't accurate. You can in fact use UIWebView to stream videos, and in some ways it's better than MPMoviePlayerController. If you tell UIWebView to request a video file (e.g. an mp4) via loadRequest:, it will open a new window and stream the video within your app. Unlike MPMoviePlayerController, the video window created by UIWebView can be rotated to landscape or portrait orientation. When the video ends, the user can close this window and return to your app.


EDIT 2: Since you can now implement a video player that rotates using MPMoviePlayerViewController, I can no longer think of a reason to use UIWebView for videos using the technique described in this answer.


Hint: Since UIWebView creates its own window to play the video, you don't even need to add the UIWebView to your view hierarchy. You can just create the UIWebView object and call loadRequest: to play the video without ever passing the object to addSubview:.

self.webView = [[[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
NSURL *url = [NSURL URLWithString:@"http://www.jonathancoulton.com/music/thingaweek/CodeMonkey.mp3"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];