Pros and cons of MPMoviePlayerController versus launching UIWebView to stream movie - iphone

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.

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

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

Related

YouTube In WebVIew - iPhone

This is very strange to me...
I have a simple WebView that loads and interacts with the user exactly like safari mobile (iPhone). Now when you visit m.youtube.com in safari, the url changes when you click on a link to something like this...
http://m.youtube.com/watch?gl=US&hl=en&client=mv-google&v=HX6SyoZ5kw8
The problem with this is I don't think that url is being used in my webview... What do I mean? The following code is used to load a url every time the user try's to click on a link, and it works, but I have a problem with Youtube...
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
NSLog(#"%#",urlString);
VideoURLTextBox.text = urlString;
return YES;
}
When I first start up the webview it loads m.youtube.com and NSLogs() it into my console, but when I decide to click on a video it fails to NSLog() therefore I don't think a new url is being loaded, but when you load m.youtube.com in safari and click on a video you load a url like above, so why does this not NSLog() in my iPhone application?
What your probably seeing is that the youtube video is running javascript to play the video, you'd have to intercept the javascript callbacks to see that. Here's a link to the youtube video player api, detailing the callbacks, https://developers.google.com/youtube/js_api_reference#SubscribingEvents
If you just want to embed a youtube video in an iOS application then use this.
Using the mobile YouTube site isn't great, I had lots of problems with it in the past and ended up making a UIWebView category. I came across your question and decided to throw it on github :-)
https://github.com/enigmaticflare/UIWebView-YouTube--iOS-Category--ARC-compliant-code
UIWebView+YouTube iOS category to simplify loading youtube videos.
Instructions
1) Add these files to your project
UIWebView+YouTube.h
UIWebView+YouTube.m
2) Initiate UIWebView for example
#property (strong, nonatomic) UIWebView *webView;
#synthesize webView = _webView
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,212,172];
//set the X,Y origin of CGRect to where you want the webView to appear.
_webView.delegate = self;
[_webView loadYouTubeVideoID:#"gR8dO7Cln6M"];
[self.view addSubView:_webView];
Good luck, hope this is what your looking for :)
Adam
m.youtube.com is one giant java web app, delegate methods won't get called, presumably because any loading is written directly to the DOM in the UIWebView. Ran into this problem at work, not sure of the fix but there may be a way to inject the source with a JS click handler.

How to play youtube videos inside iPhone app from the video url?

I am working on a project where I have to play some videos in my iPhone app. Is there any way so that I can play the videos directly using youtube video URL? I have to use lots of videos and I don't want to save them in my local iphone database. I am very new to this field.
In my project there will be a list of cancer disease. I mean blood cancer, bladder cancer, brain cancer etc. If a user clicks a particular cancer disease, then there should be a video regarding that cancer in the next view. I want to play that video directly (or something like that) using url. It should be like youtube video (streaming video). Is it possible?
This has been discussed many times on SO. Alternatively, theres an Open Source Project allowing this, that I recommend:
https://github.com/larcus94/LBYouTubeView
Add following frameworks: a) AVFoundation b) MediaPlayer
and write these import statements :
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
and then write code whenever you want
NSURL *fileURL = [NSURL URLWithString:#"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];

Adding videos to application

I want to store large number of videos in my application's directory. Will it make my application slow? Or will it take time to launch the application. I am loading videos only when necessary.
It will make large size of your application which is not good...& it will also take time to launch in place of save all ur video u can make user to download that video from server and after that it will be saved in ur doc directory so ur app will be not of large size.............
Where is it located locally
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:#"videoname" ofType:#"mov" inDirectory:#""]];
and play with that
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[[player view] setFrame:[self.view bounds]]; // Frame must match parent view
[self.view addSubview:[player view]];
[player play];
[player release];
EDIT:-
The exact answer to this question is NO, it will not make your App Slow,But yeah it ill take some time to load the videos in your App,Time will depend upon number of videos you are loading in your APP. The solution to this is you have to use blocks for ALAssetLibaries.
Have a look on this ALAssetLibary Apple's class reference.
It may help you if not do contact me. Thanks :)

Open movie stream in another view

I have in my app a tab view with many tabs, and in one of them I have a button that when is clicked, I want to show a movie stream in a view. I have this code:
NSString *moviePath = #"http://10.0.0.4/prog_index.m3u8";
theMovie = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:moviePath]];
[theMovie.view setFrame:CGRectMake(0, 0, (self.view.frame.size.width), (self.view.frame.size.height))];
theMovie.view.backgroundColor = [UIColor grayColor];
theMovie.view.tag = 9999;
[self.view addSubview:[theMovie view]];
[theMovie play];
The view appears, but the video doesn't start. What is wrong?
You're passing to the MPMoviePlayerController an URL pointing to a m3u8 file. That is a playlist. To play media with MPMoviePlayerController, you have to set it an actual video file. You should parse the playlist to get the link to the real video and then init your MPMoviePlayerController with the real link.
Check M3U - Wikipedia and MPMoviePlayerController reference
Also check this related question
EDIT: Thanks to Hugo Silva, I realized that MPMoviePlayerController is able to play live streams in m3u8 format, since I've not seen anything wrong in your code, I suggest you to check if it's a problem of your stream. Try using one of the samples provided by Apple. Also make sure that your stream meets Apple's requirements for HTTP Streaming

playing stream video using UIWebView issue

I'm currently developing an ipad application that will play video from wowza streamiong server.
Plainly saying, I load video into UIWebView as follows:
NSURL *videoURL = [NSURL URLWithString:#"http://fishki.tv:1935/vod/mp4:20100731194117.mp4/playlist.m3u8"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:videoURL];
UIWebView * WView = [[UIWebView alloc] init];
WView.frame = CGRectMake(0,0,300,200);
[self.view addSubview:WView];
[WView loadRequest:requestObj];
It plays video ok (you can try yourself), BUT when i try to load another video (or even the same one) by changing videoURL and calling loadRequest method again, it plays only sound without video.
I'm starting to think that it is a bug in ipad simulator because I was unable to fix it anyhow.
I would be glad to hear any suggestions because I also was unable to find similar problem on the web. Thanks in advance.
The web view behaves erratically on Simulator. Can you try the same on a regular device? I believe it might actually be working.