2011-05-07 200 views
2

可能重复:
Embedding YouTube videos on iOSiOS中嵌入YouTube视频

我试图嵌入iOS上的YouTube视频,这样当用户点击一个按钮,YouTube的视频没有显示离开应用程序,一旦视频完成,用户将返回到应用程序。我在网上找到了一个教程,说明如何做到这一点,并且我遵循了它。我创建了一个YouTubeView:

#import "YouTubeView.h" 


@implementation YouTubeView 

- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame { 
    self = [super init]; 
    if (self) 
    { 
     // Create webview with requested frame size 
     self = [[UIWebView alloc] initWithFrame:frame]; 

     // HTML to embed YouTube video 
     NSString *youTubeVideoHTML = @"<html><head>\ 
     <body style=\"margin:0\">\ 
     <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \ 
     width=\"%0.0f\" height=\"%0.0f\"></embed>\ 
     </body></html>"; 

     // Populate HTML with the URL and requested frame size 
     NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString, frame.size.width, frame.size.height]; 

     // Load the html into the webview 
     [self loadHTMLString:html baseURL:nil]; 
    } 
    return self; 
} 

#pragma mark - 
#pragma mark Cleanup 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

@end 

,我实例化这样的:(情况2)

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    SongInfoTableVC *infoVC; 
    YouTubeView *youTubeView; 
    switch (buttonIndex) { 
     case 0: 
      NSLog(@"hello"); 
      [self setLyrics]; 
      break; 
     case 1: 
      infoVC = [[SongInfoTableVC alloc] initWithStyle:UITableViewStyleGrouped]; 
      [infoVC setBook:data]; 
      [infoVC setTitle:@"Song Info"]; 
      [[self navigationController] pushViewController:infoVC animated:YES]; 
      [infoVC release]; 
      break; 
     case 2: 
      youTubeView = [[YouTubeView alloc] 
             initWithStringAsURL:@"http://www.youtube.com/watch?v=xli2E2i8GZ4" 
             frame:CGRectMake(0, 0, 320, 480)]; 

      [[[self navigationController] view] addSubview:youTubeView]; 
      break; 
     default: 
      break; 
    } 
} 

但这带来了一个空白屏幕。我究竟做错了什么? 下面是截图: enter image description here

由于在设备上

回答

4

尝试测试。我不认为这种方法在模拟器中是受支持的。

在这里看到:Embedding YouTube videos on

+0

谢谢,我尝试过了在设备上,它的工作原理那里! – user635064 2011-05-08 06:31:10

+0

你好,我有同样的问题,现在我成功地在设备上播放视频,但还有另一个问题,视频只播放在IOS版本5.1.1,如果我想播放视频在下面的IOS版本5.1.1我该怎么办? – Birju 2012-09-18 07:30:54