UIWebView YouTube嵌入视频未加载

时间:2014-03-01 10:26:03

标签: ios objective-c uiwebview youtube

我在通过我正在制作的iOS应用中的UIWebView嵌入视频时遇到了一些麻烦。 iframe播放器加载完全正常,但是当我按下播放按钮时,白色微调器会出现一秒然后消失,只留下一个黑盒子如果我触摸黑盒子我会得到标题和'i'按钮,但是视频永远不会开始。

我发现播放视频的唯一方法是点击右上方的“i”按钮查看信息然后再次按下该按钮将触发全屏播放器。我无法弄清楚为什么它不能在第一次出版时播放。

以下是我用来创建HTML&嵌入YouTube视频

- (void)setupHTMLString
{
    //NSData *data = [[NSData alloc] init];
    NSString *imageString = [NSData htmlForJPEGImage:[UIImage imageWithData:self.post.thumbnail]];
    NSString *htmlString = @"<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\"></head><body>";
    htmlString = [htmlString stringByAppendingString:@"<div id=\"content\" style=\"width:304px\">"];
    NSString *titleString = [NSString stringWithFormat:@"<h1>%@</h1> ", self.post.title];
    NSString *authorString = [NSString stringWithFormat:@"<h3>By %@</h3>", [self returnAuthorName]];
    NSString *contentString = [NSString stringWithFormat:@"<p>%@</p>", [self createStringWithURLsFromString:self.post.content]];
    NSString *postString = [NSString stringWithFormat:@"%@ %@ %@ %@", imageString, titleString, authorString, contentString];
    htmlString = [htmlString stringByAppendingString:postString];
    htmlString = [htmlString stringByAppendingString:@"</div?></body></html>"];


NSLog(@"%@", htmlString);

// UIWebView uses baseURL to find style sheets, images, etc that you include in your HTML.
NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[self.webView loadHTMLString:htmlString baseURL:bundleUrl];

self.webView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, INSETHEIGHT, 0);
self.webView.scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, INSETHEIGHT, 0);
}

- (NSString *)createStringWithURLsFromString:(NSString *)string
{
    NSString *regexToReplaceRawLinks = @"(?:https?:)?//(?:[^.]*\\.)?youtu(?:\\.be|be\\.com)(?:/|/embed/|/v/|/watch/?\?(?:.+&)?v=)([\\w-]{11})";



    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexToReplaceRawLinks
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];


    NSString *modifiedString = [regex stringByReplacingMatchesInString:string
                                                               options:0
                                                                 range:NSMakeRange(0, [string length])
                                                          withTemplate:@"<iframe class=\"youtube-player\" type=\"text/html\" width=\"320\" height=\"180\" src=\"http://www.youtube.com/embed/$1\" frameborder=\"0\"></iframe>"];


    //NSLog(@"%@", modifiedString);

    return modifiedString;
}

3 个答案:

答案 0 :(得分:2)

- (void)viewDidLoad {

[super viewDidLoad];

NSString *embedCode = @"<iframe width=\"265\" height=\"140\" src=\"http://www.youtube.com/embed/NUMBERSANDLETTERS\" frameborder=\"0\" allowfullscreen></iframe>";
[[self webview] loadHTMLString:embedCode baseURL:nil];
}

答案 1 :(得分:0)

在“urlStr”中传递YouTube视频的网址。

//In .h file
UIWebView *videoView;

// In .m file

videoView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 385)];

[self embedYouTube :urlStr  frame:CGRectMake(0, 0, 320, 385)];

[self.view addSubview:videoView];

// methos to embed URL in HTML & load it to UIWebView

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

}

注意:在Simulator上运行时,UIWebView for YouTube视频无法加载,无法在设备上完美运行。

答案 2 :(得分:0)

您无需做任何事情。这是Google最近推出的一个错误。谷歌修复了这个错误,它刚刚部署。如果你重新尝试你的代码,它应该工作。

如果您有兴趣,可以在此处找到缺陷的链接: https://code.google.com/p/gdata-issues/issues/detail?id=6061