2

我无法播放这种格式的视频。

http://www.youtube.com/embed/lNOMZoF9VlM?rel=0

我的 html 字符串。

NSString *html = [NSString stringWithFormat:@"<html><head>\
                              <body style=\"margin:0\">\
                              <embed id=\"yt\" src=%@ type=\"application/x-shockwave-flash\" \
                              width=\"120\" height=\"120\"></embed>\
                              </body></html>",str];

请回复。

4

3 回答 3

2

试试这个它对我有用......

    CGRect frame = CGRectMake(40, 55, width, height);


    NSString *embedHTML =[NSString stringWithFormat:@"\
                          <html><head>\
                          <style type=\"text/css\">\
                          body {\
                          background-color: transparent;\
                          color: blue;\
                          }\
                          </style>\
                          </head><body style=\"margin:0\">\
                          <iframe height=\"220\" width=\"400\" src=\"http://www.youtube.com/embed/lNOMZoF9VlM\"></iframe>\
                           </body></html>"];

    webview = [[UIWebView alloc] initWithFrame:frame];

    webview.backgroundColor = [UIColor clearColor];

    webview.scrollView.scrollEnabled =NO;

    [webview loadHTMLString:embedHTML baseURL:nil];

    [self.view addSubview:webview];
于 2013-09-25T08:12:39.750 回答
0

我实现了如下所示的 Youtube 视图。重要的是,这只能与 youtube 的embed-url的 url 字符串一起使用,它应该如下所示:

<iframe width="420" height="315" src="http://www.youtube.com/embed/ntQDyLpoLWM" frameborder="0" allowfullscreen></iframe>

(上面的所有代码都有一个问题type="application/x-shockwave-flash"吗?)

这是我的代码:

#import "YoutubeView.h"

@implementation YoutubeView

- (id)initWithFrame:(CGRect)frame andURL:(NSString *)url
{
  self = [super initWithFrame:frame];
  if (self) 
  {
    urlString = [url retain];
    webView = [[UIWebView alloc] initWithFrame:self.bounds];
    [webView.scrollView setBounces:NO];
    [self addSubview:webView];
  }
  return self;
}

-(void)layoutSubviews
{
  webView.frame = self.bounds;
  NSMutableString *html = [[NSMutableString alloc] init];
  [html appendString:@"<html>"];
  [html appendString:@"<head>"];
  [html appendString:@"<meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\">"];
  [html appendString:@"</head>"];
  [html appendString:@"<body style=\"border:0; margin:0; padding:0;\">"];
  [html appendFormat:@"<iframe width=\"%d\" height=\"%d\" src=\"%@\" frameborder=\"0\" allowfullscreen>", (int) roundf(self.bounds.size.width), (int) roundf(self.bounds.size.height), urlString];
  [html appendString:@"</iframe>"];
  [html appendString:@"</body>"];
  [html appendString:@"</html>"];
  [webView loadHTMLString:html baseURL:nil];
  [html release];
}

-(void)dealloc
{
  [webView release];
  [urlString release];
  [super dealloc];
}

@end
于 2012-08-31T07:21:49.233 回答
0

您不能在 WebView 中将 youtube 视频直接播放到您的 iPhone。首先,您需要转换为嵌入的 HTML,然后您就可以轻松播放 youtube 视频。

            UIWebView *webview=[[UIWebView alloc] initWithFrame:CGRectMake(vid.x_Pos, vid.y_Pos, vid.width,vid.height)];
            webview.backgroundColor = [UIColor clearColor];

            embedHTML = @"\
            <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/embed/lNOMZoF9VlM?rel=0\" type=\"application/x-shockwave-flash\" \
            width=\"%0.0f\" height=\"%0.0f\"></embed>\
            </body></html>";

            NSString *html = [NSString stringWithFormat:embedHTML, vid.width, vid.height];
            [webview loadHTMLString:html baseURL:nil];

            [portraitView addSubview:webview];
            [webview release];

如果您还有其他问题,请告诉我。

于 2012-08-31T06:36:33.077 回答