在嘗試通過創建HTML字符串並通過iframe
解析藤蔓來解決此問題之後,我無法實現檢索.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];
}
}
這是我的葡萄樹播放器代碼:在您的viewDidLoad
或viewWillAppear
根據自己的喜好
-(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];
}
調用此:
NSString * yourVineLink = @"https://vine.co/v/.......";
[self loadVine:yourVineLink];
通過遵循Ray Wenderlich教程,確保您已將正確的文件和導入語句添加到項目中非常重要。如果你這樣做了,這應該都可以正常工作(除非Vine決定改變它的meta標籤/ .mp4鏈接的位置)。
注:您可以在您的方法名稱中使用URL,但我喜歡NSString,這只是我的風格。
享受。