使用 MPMoviePlayerController 而不是 UIWebView 播放 YouTube 视频

我正在尝试使用 MPMoviePlayerController 流一些 youTube 视频,但是我遇到了一些问题。我使用的代码非常简单,我可以玩。通过向 initWithContentURL 传递一个 URL 来播放 m4v 视频。当我启动电影播放器时,播放器会出现,但大约20秒后就消失了。当我在模拟器中尝试它时,我得到一个警报视图,表明服务器配置不正确。是否有一个论点,我需要通过与网址,以获得一个特定类型的视频饲料从谷歌?

NSURL *videoURL = [NSURL URLWithString:@"http://www.youtube.com/v/HGd9qAfpZio&hl=en_US&fs=1&"];
MPMoviePlayerController *moviePlayer;
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];


[moviePlayer play];

我还尝试了以下 URL Http://www.youtube.com/watch?v=hgd9qafpzio

我还看到了参数 & format = 1,并试图将它添加到两个字符串的末尾,但没有找到。

77168 次浏览

The only way to have a youtube video play inside your own app is to create a UIWebView with the embed tag from Youtube for the movie you want to play as the UIWebView's content. UIWebView will detect that the embedded object is a Youtube link, and the web view's content will be the youtube preview for the video. When your user clicks the preview, the video will be shown in an MPMoviePlayerController. This is the technique described at the link that Muxecoid provided (how to play youtube videos within an application), and this is (as far as I know of) the only way to have a youtube video play within an application. You can still have the Youtube application launch by passing the youtube URL to -[UIApplication openURL:], but of course this closes your own application which is often undesirable.

Unfortunately, there's no way to directly play a youtube video with MPMoviePlayerController because youtube does not expose direct links to the video files.

If you are using Code:

- (void)embedYouTube:(NSString*)url frame:(CGRect)frame {
NSString* embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];
if(videoView == nil) {
videoView = [[UIWebView alloc] initWithFrame:frame];
[self.view addSubview:videoView];
}
[videoView loadHTMLString:html baseURL:nil];
}

Source:

Just refer this link

Make sure that you test it on device and not on the simulator. Since simulator will always display question mark with blue box (it doesn't have quick-time player).

I just stumbled across someone who was able to place a YouTube video inside an MPMoviePlayerController. It does appear possible now.

LBYouTubeView

As I wrote above in my comment I had to do this in a project I was working on. I've solved this problem and submitted the code on github.

You can check the source and fork it here.

It basically takes a youtube url and gets all the iOS compatible video urls.

Hope this is to any help!
Cheers

It's as simple as taking the src element of the iframe for the new embed code and calling

[self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];

To play you tube videos you need to extract the url before passing the url into MPMoviePlayer. You can't play it directly.

My github repo has a demo implementation of this.

See:

https://github.com/DpzAtMicRO/IOSYoutubePlayer

Try it, this makes it possible to load and play youtube video directly in MPMoviePlayerlike any other video and is pretty good approach too.

EDIT: Make sure that you go through the Readme.md well before using this in your project

Check the following link. I think it will help you.

https://mobiletechfeast.blogspot.in/2014/08/youtube-player-for-ios.html

There is an official way to embed YouTube video in iOS application which is provided by Google.

Install library via cocoapods: pod "youtube-ios-player-helper"

import by: #import “YTPlayerView.h” Use UIView and set class to YTPlayerView or create object of YTPlayerView programatically.

Load video: [self.playerView loadWithVideoId:@"M7lc1UVf-VE"];

There is also playVideo and stopVideo methods available. For more info visit this Link