【问题标题】:UIWebView loadHTMLString for playing video fileUIWebView loadHTMLString 用于播放视频文件
【发布时间】:2012-04-04 17:47:00
【问题描述】:

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

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

【问题讨论】:

    标签: iphone ios ipad uiwebview youtube


    【解决方案1】:

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

    祝你好运!

    【讨论】:

    • 感谢 Heliem,我正在 IOS 5.1 设备上测试它。
    【解决方案2】:

    试试这个代码:

      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”将无法正常工作。它需要本地化。

    迈克

    【讨论】:

      【解决方案3】:

      试试

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

      希望对你有帮助

      【讨论】:

      • 感谢 Daniel 的帮助,但它不工作,我得到 mmap(size=2147487744) failed (error code=12) *** error: can't allocate region while我正在尝试youtu.be/NWCHSD1UKN4
      • 它显示加载视图的时间太长了。像应用程序挂起。你能建议更多吗?谢谢
      • 感谢 Daniel 在我对自己的代码进行了一些更改并集成了您的代码之后,它现在可以工作了。谢谢...
      猜你喜欢
      • 2011-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多