Monday, February 21, 2011

Embedding youtube video in a webview


This post describes how you can embed you tube video in a webView which is in a table cell & on row selection autoplay youtube video

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

UIWebView  *wbView = (UIWebView *)[cell.contentView viewWithTag:1];
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, 64.0, 64.0];
[wbView loadHTMLString:html baseURL:nil];



}

 "url" is the youtube video url


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [category_table cellForRowAtIndexPath:indexPath];
UIWebView  *wbView = (UIWebView *)[cell.contentView viewWithTag:1];
UIButton *btn = [self findButtonInView:wbView];
[btn sendActionsForControlEvents:UIControlEventTouchUpInside];
}


// detecting the play button on the you tube video thumbnail

- (UIButton *)findButtonInView:(UIView *)view {
UIButton *button = nil;
if ([view isMemberOfClass:[UIButton class]]) {
return (UIButton *)view;
}
if (view.subviews && [view.subviews count] > 0) {
for (UIView *subview in view.subviews) {
button = [self findButtonInView:subview];
if (button) return button;
}
}
return button;
}


- (void)webViewDidFinishLoad:(UIWebView *)_webView {
UIButton *button = [self findButtonInView:_webView];
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
}


3 comments:

  1. what is the category_table mean???

    ReplyDelete
  2. is it possible to play yotube embed video to mpmovie player or is it posible to avoid full screeen in web view

    ReplyDelete
  3. i am loading youtube video on web view when i am trying to play it goes to full screen mode it needs to be wher i clicked and same frame

    ReplyDelete