【问题标题】:Get the Vine .mp4 source - iOS获取 Vine .mp4 源 - iOS
【发布时间】:2015-09-07 02:53:02
【问题描述】:
【问题讨论】:
标签:
javascript
ios
mp4
vine
【解决方案1】:
在尝试使用 HTML/JS 通过创建 HTML 字符串并通过 iframe 解析 vine 来解决此问题后,我无法获得检索 .mp4 链接的预期结果。
我通过以下方式克服了这个问题:
转到Ray Wenderlich 解析 HTML 教程,并按照教程进行操作。您需要的最重要的部分是将 hpple 文件安装到您的项目(通过 Ray Wenderlich 教程找到)并参考 libxml2。
-
使用此代码进行解析以获取 MP4:
-(void)loadVine: (NSString *) vineLink {
//create the string to store the result
NSString * url = nil;
// Create a URL from the String passed in
NSURL *vineURL = [NSURL URLWithString:vineLink];
NSData *vineHTMLdata = [NSData dataWithContentsOfURL:vineURL];
TFHpple *vineParser = [TFHpple hppleWithHTMLData:vineHTMLdata];
// This is the most important part of your query
// The vine .mp4 path is in the `<meta>` tag as show below
NSString *vineXpathQueryString = @"//meta[@property='twitter:player:stream']";
NSArray *vineNodes = [vineParser searchWithXPathQuery:vineXpathQueryString];
for (TFHppleElement *element in vineNodes) {
//this sets your `NSString * url` to be the result
url = [element objectForKey:@"content"];
//print it out in log to display your .mp4 link
NSLog(@"What is the vine? %@", url);
//now pass it to your player
[self autoPlayVine:url];
}
}
-
这是我的 vine 播放器代码:
-(void) autoPlayVine :(NSString *) link{
NSURL * url = [NSURL URLWithString:link];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
self.moviePlayer.controlStyle = MPMovieControlStyleDefault;
self.moviePlayer.shouldAutoplay = YES;
// set the frame to be full screen - needed otherwise you may only get the audio as there is no frame size set, but the movieplayer has been added to the view
self.moviePlayer.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
// below enables either continuous loop or delete for one time play
self.moviePlayer.repeatMode = YES;
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
}
-
根据您的喜好,在您的 viewDidLoad 或 viewWillAppear 中调用:
NSString * yourVineLink = @"https://vine.co/v/.......";
[self loadVine:yourVineLink];
通过遵循 Ray Wenderlich 教程,确保您已将正确的文件和导入语句添加到您的项目中非常重要。如果你这样做了,这一切都应该正常工作(除非 Vine 决定更改其元标记/.mp4 链接的位置)。
注意:您可以在方法名称中使用 URL,但我喜欢 NSString,这只是我的风格。
享受吧。