2016-04-07 120 views
0

玩我想下載或文檔目錄保存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]; 
} 
+0

的的MPMoviePlayerController類在IOS 9.正式棄用(MPMoviePlayerViewController類也正式棄用。)爲了在IOS 9播放視頻內容和更高版本,而是使用AVPictureInPictureController或AVPlayerViewController類從AVKit框架,或來自WebKit的WKWebView類。 –

+0

您無法直接從網址下載YouTube視頻,也不能從app store購買safe.apple不允許從第三方下載視頻 –

+0

@SunnyShah:那麼從URL下載管狀視頻的其他選項是什麼? –

回答

2

播放視頻url爲youtube所以它不會被下載或保存在本地

使用此爲y outube

我使用了這個項目中的類:https://github.com/larcus94/LBYouTubeView它適用於我。我可以下載YouTube視頻。

LBYouTubeExtractor *extractor = [[[LBYouTubeExtractor alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:(@"http://www.youtube.com/watch?v=%@"), self.videoID ]] quality:LBYouTubeVideoQualityLarge] autorelease]; 
[extractor extractVideoURLWithCompletionBlock:^(NSURL *videoURL, NSError *error) { 
    if(!error) { 
     NSLog(@"Did extract video URL using completion block: %@", videoURL); 

     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
      NSData *data = [NSData dataWithContentsOfURL: videoURL]; 
      NSString *pathToDocs = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
      NSString *filename = [NSString stringWithFormat:(@"video_%@.mp4"), self.videoID ]; 
      [data writeToFile:[pathTODocs stringByAppendingPathComponent:filename] atomically:YES]; 
      NSLog(@"File %@ successfully saved", filename); 
     }); 
    } else { 
     NSLog(@"Failed extracting video URL using block due to error:%@", error); 
    } 
}]; 
+0

我得到一個錯誤'由於錯誤,使用塊提取視頻URL失敗:錯誤域= LBYouTubeExtractorErrorDomain代碼= 2「無法找到流URL。 UserInfo = {NSLocalizedDescription =找不到流的網址} ' –

+0

其已知問題dw .. 請通過https://github.com/rg3/youtube-dl/issues/427 –

+0

它可以在模擬器中工作? –

相關問題