【问题标题】:Extract YouTube Video Link Using YouTube API使用 YouTube API 提取 YouTube 视频链接
【发布时间】:2013-03-06 20:07:39
【问题描述】:

我开发了一个应用程序来加载用户的视频,当他们点击表格视图中的单元格时,它会转到另一个包含大量不同信息的视图控制器。

我正在尝试从特定视频中提取实际的 YouTube 链接。我打印出我得到的链接,它们以这种形式出现。

https://www.youtube.com/v/Xf5pXlZJr1U?version=3&f=user_uploads&app=youtube_gdata

但我希望它是这种形式。

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

原因是我希望人们通过社交媒体分享链接,但当它采用第一种格式时会显得很奇怪。

有什么方法可以轻松转换链接?


示例代码和答案编辑

GDataEntryBase *entry2 = [[feed entries] objectAtIndex:indexPath.row];
NSString *title = [[entry2 title] stringValue];
NSArray *contents = [[(GDataEntryYouTubeVideo *)entry2 mediaGroup] mediaContents];
GDataMediaContent *flashContent = [GDataUtilities firstObjectFromArray:contents withValue:@"application/x-shockwave-flash" forKeyPath:@"type"];
NSString *tempURL = [flashContent URLString];

NSArray *thumbnails = [[(GDataEntryYouTubeVideo *)entry2 mediaGroup] mediaThumbnails];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[thumbnails objectAtIndex:0] URLString]]];
UIImage *image = [UIImage imageWithData:data];

GDataYouTubeMediaGroup *mediaGroup = [(GDataEntryYouTubeVideo *)entry2 mediaGroup];
GDataMediaDescription *desc2 = [mediaGroup mediaDescription];
NSString *mystring = [desc2 stringValue];

NSArray *listItems = [tempURL componentsSeparatedByString:@"/"];
NSString *NewURL = @"";

NewURL = [NewURL stringByAppendingString:[listItems objectAtIndex:0]];
NewURL = [NewURL stringByAppendingString:@"//"];
NewURL = [NewURL stringByAppendingString:[listItems objectAtIndex:2]];
NewURL = [NewURL stringByAppendingString:@"/"];
NewURL = [NewURL stringByAppendingString:@"watch"];
NewURL = [NewURL stringByAppendingString:@"?v="];

NSArray *listItems2 = [[listItems objectAtIndex:4] componentsSeparatedByString:@"?"];    
NewURL = [NewURL stringByAppendingString:[listItems2 objectAtIndex:0]];

【问题讨论】:

  • 添加一些代码和你使用的 youtube api
  • 你去。我给了一些代码。
  • api??请包含api
  • 我使用了标准的 GData Api,您可以从 google 网站下载。
  • 最好在有问题的地方添加它。没有人会搜索并找到用于回答的 api

标签: iphone ios youtube-api


【解决方案1】:

如果您查看 NSString 文档,那么您会发现许多有用的方法供您自己使用,例如 componentsSeparatedByString 根据某些特定字符将字符串分成两个不同的块 & stringByAppendingString 用于将字符串附加到现有字符串。这些方法使用起来并不难,根据自己的需要使用即可。

【讨论】:

    猜你喜欢
    • 2016-07-22
    • 2014-04-10
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 2013-04-23
    • 1970-01-01
    相关资源
    最近更新 更多