在不使用UIWebView的情况下在iPhone应用中播放YouTube视频?

Yuv*_*j.M 31 youtube iphone mpmovieplayercontroller uiwebview ios

我想从我的iPhone应用程序播放YouTube视频.我必须尝试使用​​以下代码在我的iPhone应用中播放YouTube视频,

[self playVideo:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q" frame:CGRectMake(20, 70, 280, 250)];

- (void)playVideo:(NSString *)urlString 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, urlString, frame.size.width, frame.size.height];
    UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
    [videoView loadHTMLString:html baseURL:nil];
    [self.view addSubview:videoView];
    [videoView release];
    NSLog(@"%@",html);
}
Run Code Online (Sandbox Code Playgroud)

但是,这段代码对我不起作用.而且我也试过MPMoviePlayerController代码是,

NSURL *fileURL = [NSURL URLWithString:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q"];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)]; 
[self.view addSubview:moviePlayerController.view];  
moviePlayerController.fullscreen = YES;  
[moviePlayerController play];
Run Code Online (Sandbox Code Playgroud)

这对我来说也不起作用.任何人都可以告诉我我做错了什么,并建议我任何想法?我想播放YouTube视频MPMoviewPlayerController而不是使用UIWebView.提前致谢.

Hlu*_*ung 10

只需使用XCDYouTubeVideoPlayerViewController.它可以无缝地将youtube视频无缝添加到您的应用中UIWebView.

它使用渐进式下载+ MoviePlayerAVFoundation框架.只需提供一个YouTube视频ID就可以了.

它支持全屏和非全屏以及WORKS ON iOS模拟器!

例:

XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"9bZkp7q19f0"];
[self presentMoviePlayerViewControllerAnimated:videoPlayerViewController];
Run Code Online (Sandbox Code Playgroud)


Max*_*eod 8

现在YTPlayerView,YouTube Data API(v3)提供了一个课程.

请查看使用YouTube帮助程序库在您的iOS应用程序中嵌入YouTube视频

它提供了有关如何安装帮助程序库以及将YTPlayerView合并到故事板中的全面说明.


Gop*_*ath 7

Mr.Yuvaraj.M您的上述代码是正确的.请在iPhone/iPod touch设备中运行该项目.youtube视频可能不支持/不在Simulator中工作.请检查一下.谢谢.