玩我想下載或文檔目錄保存YouTube視頻然後發揮該視頻使用MPMoviePlayerViewController。但我不知道爲什麼我的代碼不工作。我無法保存或下載文件的目錄視頻和文檔目錄
我用下面這段代碼:
prgrma mark-下載或保存
-(IBAction)onclick_download:(id)sender
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Downloading Started");
NSString *urlToDownload = @"youtubeurl";
NSURL *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if (urlData)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"thefile.mp4"];
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:YES];
NSLog(@"File Saved !");
});
}
});
}
編譯mark-從當你的鏈接,你所提供的文檔目錄
-(IBAction)onclick_playvideolocally:(id)sender
{
[self playvideolocally];
}
-(void)playvideolocally
{
NSURL *videoUrl;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSLog(@"files array %@", filePathsArray);
NSString *fullpath;
for (NSString *apath in filePathsArray)
{
fullpath = [documentsDirectory stringByAppendingPathComponent:apath];
videoUrl =[NSURL fileURLWithPath:fullpath];
}
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoUrl];
[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];
}
的的MPMoviePlayerController類在IOS 9.正式棄用(MPMoviePlayerViewController類也正式棄用。)爲了在IOS 9播放視頻內容和更高版本,而是使用AVPictureInPictureController或AVPlayerViewController類從AVKit框架,或來自WebKit的WKWebView類。 –
您無法直接從網址下載YouTube視頻,也不能從app store購買safe.apple不允許從第三方下載視頻 –
@SunnyShah:那麼從URL下載管狀視頻的其他選項是什麼? –