2013-10-02 22 views
0

如何使用YouTube上的gdata獲取單個視頻信息。 我知道我可以使用此代碼得到一個播放列表: 但是代碼不工作就是讓單個視頻的數據如何獲取單個Youtube視頻的GDATA?

NSURL *blogURL = [NSURL URLWithString:@"https://gdata.youtube.com/feeds/api/playlists/PL85E8F3CC22BCEDC6?v=2&alt=jsonc"]; 

NSData *jsonData = [NSData dataWithContentsOfURL:blogURL]; 

NSError *error = nil; 

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; 


NSDictionary *data1 = [dataDictionary objectForKey:@"data"]; 

NSDictionary *item = [data1 objectForKey:@"items"]; 

for (NSDictionary *video in item) { 
    NSDictionary *title = [video objectForKey:@"video"]; 
    NSArray *titleArray = [title objectForKey:@"title"]; 

    NSLog(@"%@",titleArray); 

    NSString*t = [title objectForKey:@"title"]; 
    NSLog(@"Title:%@", t); 

    ... 
} 

但我不知道如何讓一個視頻。 任何幫助將不勝感激... 謝謝...

+0

的可能重複[加載的YouTube GData飼料的單個視頻的ID](http://stackoverflow.com/questions/4112414/load-youtube-gdata-feed-for-a-single-video-by-id) – picciano

回答

0

要獲得與V2單個視頻的飼料,你會替換在第一行的URL像這樣的東西:

https://gdata.youtube.com/feeds/api/videos/tge2BfiIXiE?v=2&alt=jsonc 

根據需要更改videoID的位置。

與此提要一起返回的JSON稍有不同,自然而然;因此,而不是創建的項目列表一個NSDictionary對象,然後遍歷它,你只想做這樣的事情:編輯

NSURL *feedURL = [NSURL URLWithString:@"https://gdata.youtube.com/feeds/api/videos/tge2BfiIXiE?v=2&alt=jsonc"]; 

NSData *jsonData = [NSData dataWithContentsOfURL:feedURL]; 

NSError *error = nil; 

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; 

NSDictionary *data1 = [dataDictionary objectForKey:@"data"];  
NSString *t = [data1 objectForKey:@"title"]; 
NSLog(@"Title:%@", t); 

NSDictionary *thumbs = [data1 objectForKey:@"thumbnail"]; 

NSURL *standardThumb = [thumbs objectForKey:@"sqDefault"]; 
NSURL *hdThumb = [thumbs objectForKey:@"hqDefault"]; 

代碼以反映意見的問題

+0

謝謝...你的代碼工作大。你知道如何從那個 – user2569812

+0

得到一個縮略圖嗎?幸運的是,縮略圖路徑也在feed中;我已經更新了上面的代碼以演示如何訪問它。由於我不是iOS程序員,我不知道你是如何實際檢索圖像的,但是一旦你擁有添加的代碼的路徑,這應該是微不足道的。 – jlmcdonald

+0

我已經嘗試使用這段代碼,但我似乎無法得到它在它不填充我的UIImageView的意義上工作:'NSURL * standardThumb = [thumbs objectForKey:@「sqDefault」]; NSURL * hdThumb = [thumbs objectForKey:@「hqDefault」]; UIImage * thumbnail = [UIImage imageWithData:[NSData dataWithContentsOfURL:hdThumb]]; [currentItem1 setImage:thumbnail]; ' – user2569812