0

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

但没有获得成功。谁能帮我 ?我在这里做错了什么?谢谢

4

3 回答 3

1

请记住,播放此类 youtube 视频UIWebView将始终在模拟器上显示白屏。您需要在设备中对其进行测试才能知道它是否真的有效。

祝你好运!

于 2012-04-04T17:53:14.337 回答
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 ”将无法正常工作。它需要本地化。

麦克风

于 2012-04-04T20:58:33.657 回答
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];  
}

希望能帮助到你

于 2012-04-04T17:50:36.647 回答