UIWebView YouTube Embed video not loading

被刻印的时光 ゝ 提交于 2019-12-08 01:22:11

问题


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

回答1:


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




回答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];
}



回答3:


Pass you URL of YouTube Video in “urlStr”.

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

}

Note : The UIWebView for YouTube video doesn’t load when you run it on Simulator, work on device perfectly.



来源:https://stackoverflow.com/questions/22113104/uiwebview-youtube-embed-video-not-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!