UIWebView loadHTMLString for playing video file

Go To StackoverFlow.com


LoadHtmlString UIWebView is not working. I want to play embeded you tube video. I have tried with Embeded HTML string. But some seems broken i am getting white screen. i Have tried with following

NSString* html = @"<embed id=\"yt\" src=\"http://www.youtube.com/v/6eK-W32IME0?fs=1&hl=en_US&enablejsapi=1\" type=\"application/x-shockwave-flash\" width=\"330\" height=\"200\"></embed>";

NSString*html = @"<iframe class=\"youtube-player\" type=\"text/html\" width=\"320\" height=\"460\" src=\"http://www.youtube.com/watch?v=oEn9g4pNW9Y&feature=youtu.be\" frameborder=\"0\"> </iframe>";

[videoView loadHTMLString:html baseURL:nil];

But does not get success. Can any one help me ? What wrong i have done here? Thanks

2012-04-04 17:47
by Mangesh



- (void)embedYouTube:(NSString*)urls frame:(CGRect)frame { 

    urls=[urls stringByAppendingString:@"&autoplay=1"];
    NSString *htmlString =[NSString stringWithFormat: @"<html><head> <meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head> <body style=\"background:#F00;margin-top:0px;margin-left:0px\"><div><object width=\"212\" height=\"212\"><param name=\"movie\" value=\"%@\">",urls];

    NSString *htm2=[NSString stringWithFormat:@"</param><param name=\"wmode\" value=\"transparent\"></param>    <embed src=\"%@\"type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"212\" height=\"212\"></embed>    </object></div></body></html>",urls];
//    NSString* embedHTML = @"<html><head> </head><body style=\"margin:0\"> <iframe title=\"YouTube video player\" class=\"youtube-player\" type=\"text/html\" width=\"%d\" height=\"%d\" src=\"%@\" frameborder=\"0\" allowFullScreen></iframe> </body></html>";  

    NSString* html = [NSString stringWithFormat:@"%@%@",htmlString,htm2];
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];


        NSRange r=[urls rangeOfString:@"v="];
        NSRange ran=NSMakeRange(r.location+2, [urls length]-r.location-2);
        NSString *vid=[urls substringWithRange:ran];
    html=[NSString stringWithFormat:@"<embed id=\"yt\" src=\"http://www.youtube.com/watch?v=%@&fs=0\" type=\"application/x-shockwave-flash\" width=\"300\" height=\"300\"></embed>", vid];
    if(webView == nil) {  
        webView = [[UIWebView alloc] initWithFrame:frame];  
        [self.view addSubview:webView];  
        [webView setDelegate:self];
    [webView loadHTMLString:html baseURL:nil];  

hope it helps

2012-04-04 17:50
by Daniel
Thanks Daniel for your help but it is not working, i am getting mmap(size=2147487744) failed (error code=12) * error: can't allocate region while i am trying with http://youtu.be/NWCHSD1UKN - Mangesh 2012-04-04 18:08
It show loading view for you tube for too much time. Like app hang. can you suggest more? thank - Mangesh 2012-04-04 18:10
Thanks Daniel After some changes in my own code and integrate your one, it is working now Thanks.. - Mangesh 2012-04-09 09:31


Keep in mind that this sort of youtube video playing through UIWebView will always show a white screen on the simulator. You need to test it in a device to know if it actually works.

Good luck!

2012-04-04 17:53
by Zalykr
Thanks Heliem, i am testing it on IOS 5.1 device - Mangesh 2012-04-04 17:55


Try this code:

  NSString *html = @"\
  <style type=\"text/css\">\
  body {\
  background-color: transparent;\
  color: white;\
  </head><body style=\"margin:0\">\
  <embed id=\"yt\" src=\"http://www.youtube.com/v/6eK-W32IME0?fs=1&hl=en_US&enablejsapi=1\" type=\"application/x-shockwave-flash\" \
  width=\"320\" height=\"200\"></embed>\

  // Load the html into the webview
  [videoView loadHTMLString:html baseURL:nil];

Also, I have found that the "youtu.be" does not work properly if your testing on an American iOS. It needs to be localized.


2012-04-04 20:58
by Mike
Thanks Mike.... I am using same.. - Mangesh 2012-04-09 09:29