Pros and cons of MPMoviePlayerController versus launching UIWebView to stream movie

試著忘記壹切 提交于 2019-12-02 15:39:11
Dan Lorenc

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

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];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!