iPhone在网络视图中播放youtube视频

时间:2012-08-31 06:30:05

标签: iphone html ios5 uiwebview

我无法以此格式播放视频。

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

请回复。

3 个答案:

答案 0 :(得分: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];

答案 1 :(得分: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];

如果您还有其他问题,请与我们联系。

答案 2 :(得分: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