UIWebView YouTube 嵌入视频未加载 [英] UIWebView YouTube Embed video not loading

查看:26
本文介绍了UIWebView YouTube 嵌入视频未加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having some trouble getting videos that I am embedding through a UIWebView in an iOS app that I am making. The iframe player loads up completely fine, but when I hit the play button the white spinner appears for a second then disappears, leaving just a black box If I touch on the black box I get the title and the 'i' button, but the video will never start.

我发现播放视频的唯一方法是点击右上角的i"按钮查看信息,然后再次按下它会触发全屏播放器.我只是不明白为什么它不会在第一次按下时播放.

The only way I have found to get the video to play is by hitting the 'i' button in the top right to see the info then pressing it again which will trigger the fullscreen player. I just can't figure out why it won't play on the first press.

这是我用来创建 HTML 的完整代码 &嵌入 YouTube 视频

Here is the full code I'm using to create the HTML & embed the YouTube video

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

推荐答案

您无需执行任何操作.这是谷歌最近引入的一个错误.谷歌修复了这个bug,刚刚部署.如果您重新尝试您的代码,它应该可以工作.

You don't have to do anything. It was a bug that Google introduced recently. Google fixed the bug and it was deployed just now. If you re-try your code, it should work.

如果您有兴趣,这里是缺陷的链接:https://code.google.com/p/gdata-issues/issues/detail?id=6061

Here is a link to the defect, if you're interested: https://code.google.com/p/gdata-issues/issues/detail?id=6061

这篇关于UIWebView YouTube 嵌入视频未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆