在 iPhone 应用程序中播放 YouTube 视频而不使用 UIWebView?

2024-04-03

我想通过我的 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);
}

但是,这段代码对我不起作用。我也尝试过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];

这对我来说也不起作用。谁能告诉我我哪里做错了并给我提出任何想法?我想播放 YouTube 视频MPMoviewPlayerController而不是使用UIWebView。提前致谢。


只需使用XCDYouTubeVideoPlayerViewController https://github.com/0xced/XCDYouTubeVideoPlayerViewController。它将 YouTube 视频无缝添加到您的应用程序中,无需UIWebView.

它使用渐进式下载+MoviePlayer and AVFoundation构架。只需提供 YouTube 视频 ID 即可开始。

它支持全屏和非全屏,适用于 iOS 模拟器!

Example:

XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"9bZkp7q19f0"];
[self presentMoviePlayerViewControllerAnimated:videoPlayerViewController];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 iPhone 应用程序中播放 YouTube 视频而不使用 UIWebView? 的相关文章

随机推荐