Mangesh
Mangesh

Reputation: 2285

UIWebView loadHTMLString for playing video file

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

Upvotes: 0

Views: 3300

Answers (3)

Mike
Mike

Reputation: 121

Try this code:

  NSString *html = @"\
  <html><head>\
  <style type=\"text/css\">\
  body {\
  background-color: transparent;\
  color: white;\
  }\
  </style>\
  </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>\
  </body></html>";


  // 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.

Mike

Upvotes: 1

Zalykr
Zalykr

Reputation: 1524

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!

Upvotes: 1

Daniel
Daniel

Reputation: 22395

try

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


    if(version<5.0)
    { 

        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];
    }
    //NSLog(html);
    if(webView == nil) {  
        webView = [[UIWebView alloc] initWithFrame:frame];  
        [self.view addSubview:webView];  
        [webView setDelegate:self];
    }  
    [webView loadHTMLString:html baseURL:nil];  
}

hope it helps

Upvotes: 0

Related Questions