在iphone上嵌入YouTube视频时出现问题

时间:2011-02-27 22:18:36

标签: iphone iframe uiwebview youtube

我正在尝试使用以下代码嵌入视频,以避免启动youtube应用并留在我的应用中:

-(void)embedYouTube:(NSString*)url frame:(CGRect)frame {  

 NSString* embedHTML = @"<iframe type=\"text/html\" width=\"64\" height=\"39\" src=\"http://www.youtube.com/embed/j8Bc7eRTdWY\" frameborder=\"0\"></iframe>";
    if(videoView == nil) {          
        videoView = [[UIWebView alloc] initWithFrame:frame];  
        [movieDescView addSubview:videoView];  
    }  
    [videoView loadHTMLString:embedHTML baseURL:nil];  
}

问题是,当我点击播放图标时,它打开youtube网站,我的uiwebview要求安装一些网络应用程序。我想阻止此网络应用弹出以全屏显示或启动视频。实际上,任何在不退出我的应用程序的情况下显示YouTube视频的解决方案都可以。

5 个答案:

答案 0 :(得分:4)

您可以使用它来加载电影(不需要生成html字符串):

movieView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 140, 100)];
movieView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.youtube.com/embed/-h4zTEwgCpQ"]];
[movieView loadRequest:request];
[self.view addSubview:movieView];


#pragma mark -
#pragma mark UIWebViewDelegate
//To check if youtube try to show the overview page of m.youtube.com or the current movie
//if the user click on the youtube logo this method stop loading the mobile page
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSURL *urlRequest = [request URL];

    if ([[urlRequest absoluteString] isEqualToString:url]) {
        return YES;
    }

    return NO;
}

您需要寻找的一件事就是使用“嵌入”链接。

答案 1 :(得分:2)

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

答案 2 :(得分:0)

如果您尝试播放视频,则不要使用嵌入。只需使用普通的视频网址,即可在iPhone的YouTube应用程序而不是网络中打开。如果它在网页上打开,它会要求安装它的网络应用程序。

您需要将HTML更改为:

<a href="http://www.youtube.com/watch?v=j8Bc7eRTdWY">Play Video</a>

或者直接将其发布到本机应用使用网址:

http://www.youtube.com/watch?v=j8Bc7eRTdWY

更多参考资料:

Apple Reference on URL Schemes

Apple Reference on Youtube Links

答案 3 :(得分:0)

提示:

stringUrl = [stringUrl stringByReplacingOccurrencesOfString:@"/watch?v=" withString:@"/embed/"];

其中stringUrl是youtube的视频链接(例如Youtube

答案 4 :(得分:0)

我花了很多时间浏览库和旧的代码片段;这个图书馆对我有用:

https://github.com/larcus94/LBYouTubeView

相关问题