如何在我的应用程序中播放Youtube链接视频?

时间:2011-05-30 11:00:27

标签: iphone youtube video-streaming

我想在我的应用程序中播放YouTube视频。我写了下面的代码,但视频无法播放,请告诉我如何播放视频,在此先感谢

NSURL *url =[NSUrl UrlWithString: @"http://www.youtube.com/watch?v=2MSMJgpmVM8&feature=grec_index"];;
    NSString *embedHTML = @"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";    
videoView = [[UIWebView alloc] initWithFrame:CGRectMake(100, 100, 320.0, 412.0)];

[videoView setOpaque:NO];
NSString *html = [NSString stringWithFormat:embedHTML,url, videoView.frame.size.width, videoView.frame.size.height];
[videoView loadHTMLString:html baseURL:nil];

[self.view addSubview:videoView];   

5 个答案:

答案 0 :(得分:0)

嘿,我不认为你需要嵌入任何东西。你可以简单地用适当的URL拍摄webView ...我已经做了一个星期了...它在Youtube上播放了视频.. Cheerzz .. !!

答案 1 :(得分:0)

请按照Link1Link2

进行操作

只需将您的网址传递给fileURL

即可

答案 2 :(得分:0)

我使用以下函数将HTML放在UIWebView

- (NSString *)YouTubeHTMLForURL:(NSString *)URLString;
{
  int w = streamWebView.frame.size.width;
  int h = streamWebView.frame.size.height;

    NSString *HTML = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
    width:%dpx;\
    height:%dpx;\
    background-color:#252525;\
    color:white;\
    }\
    </style>\
    </head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
    width=\"%d\" height=\"%d\"></embed>\
    </body></html>";
    return [NSString stringWithFormat:HTML, w, h, URLString, w, h];
}

答案 3 :(得分:0)

UIWebView  *wbView = (UIWebView *)[self.view viewWithTag:1];
NSString *embedHTML = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
    background-color: transparent;\
    color: white;\
    }\
    </style>\
    </head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
    width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>";

    NSString *html = [NSString stringWithFormat:embedHTML,url, 64.0, 64.0];
    [wbView loadHTMLString:html baseURL:nil];

  "url" is your video url

@Mike video plays only on device not on simulator

答案 4 :(得分:0)

以下代码应该有效 -

但请注意,youtube仅适用于设备,您是否在设备上进行了检查。

UIWebView  *wbView = (UIWebView *)[self.view viewWithTag:1];
NSString *embedHTML = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
    background-color: transparent;\
    color: white;\
    }\
    </style>\
    </head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
    width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>";

    NSString *html = [NSString stringWithFormat:embedHTML,url, 64.0, 64.0];
    [wbView loadHTMLString:html baseURL:nil];