2011-05-07 199 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