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.
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.
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;
}
Pass you URL of YouTube Video in “urlStr”.
Note : The UIWebView for YouTube video doesn’t load when you run it on Simulator, work on device perfectly.
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.
Here is a link to the defect, if you're interested: https://code.google.com/p/gdata-issues/issues/detail?id=6061