用于播放视频文件的UIWebView loadHTMLString

时间:2012-04-04 17:47:01

标签: iphone ios ipad uiwebview youtube

LoadHtmlString UIWebView不起作用。我想播放嵌入你管视频。我尝试过使用嵌入式HTML字符串。但是有些人看起来很破,我正在看白屏。我尝试了以下

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

但是没有成功。谁能帮我 ?我在这做了什么错?感谢

3 个答案:

答案 0 :(得分:1)

请注意,通过UIWebView播放的这类YouTube视频将始终在模拟器上显示白屏。您需要在设备中对其进行测试,以确定它是否真的有效。

祝你好运!

答案 1 :(得分:1)

试试这段代码:

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

此外,我发现如果您在美国iOS上进行测试,“ youtu.be ”无效。它需要本地化。

麦克

答案 2 :(得分:0)

尝试

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

希望有所帮助