views:

6768

answers:

2

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.

+4  A: 

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];
Dan Lorenc
OK. So my real choice is between the YouTube app and MPMoviePlayerController. Thanks--the latter choice looks pretty easy indeed. I'll probably go ahead and try both ways.
Nosredna
Dan - Can you edit your answer so that the two code lines are both formatted as "code". At least for me on Firefox 3.5 I see the NSString line as formatted code but the MPMoviePlayerController line is not. Just a suggestion to make it look better for the next guy.
Chris Markle
+13  A: 

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.

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];
cduhn
I found using UIWebView to be the best method for showing a video in portrait orientation. However, I DID have to add the UIWebView to the view hierarchy when testing in the sim v3.1.3. I had trouble trying to play a .mov file but opening it in the Quicktime player and doing a "save as" for iPhone gave me a usable .m4v version.
Jason
I can also add that the movie player launched by UIWebView can't handle as many video formats as MPMoviePlayerController. For example, JPEG-encoded movies that many digital cameras record are just presented as a play button with a crossed line above it (indicating a broken movie).So it's either interface orientation or all video formats.
alleus
@cduhn: Can you confirm this code for 4.0? I get "setting movie path:(null)" this way. Although it still works in 3.2 - very confusing
hecta
Actually I did have a syntax error. My brackets were mismatched on the UIWebView initialization line. It should have failed to compile before. I've now edited my example and directly pasted in code from a sample that I've confirmed to work in iOS 4 when it's built for 3.1.3.
cduhn
Still getting path (null) with my video - your mp3 works. So it might be an now unsupported encoding on my side. Although I already tried several H.264 - Could you check what happens if you try to load a video? That would be much appreciated
hecta
Same code works for video. I tested it with http://mirrors.creativecommons.org/movingimages/Building_on_the_Past.mp4
cduhn
You're right, thank you! I must be really having trouble encoding it right. Tried it in XMediaRecode with the iPhone Presets in Windows7 and Quicktime X and iTunes exports for iPhone formats.
hecta
I tested my videos locally, eg. hooking up the path in xcode with my local webserver and they show up. But they don't on two different servers, how can this be server related?
hecta
Not sure. Perhaps the servers aren't returning the right MIME type. You should post this as a question.
cduhn
Ok thanks for your help, - you can also write this answer on my old question, which I had a bounty on, and I will accept your answer...- new question is here http://stackoverflow.com/questions/3102583/iphone-uiwebview-safari-app-ios-4-video-movie-server-issue
hecta