【问题标题】:I'm unable to save or download video in document directory and play from document directory我无法在文档目录中保存或下载视频并从文档目录播放
【发布时间】:2016-07-27 18:40:53
【问题描述】:

我想在文档目录中下载保存 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 !");
            });
        }
        
    });
    
}

pragma 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 及更高版本中播放视频内容,请改用 AVKit 框架中的 AVPictureInPictureController 或 AVPlayerViewController 类,或 WebKit 中的 WKWebView 类。
  • 你不能直接从 url 下载 youtube 视频,也不是应用商店安全的。苹果不允许从第三方下载视频
  • @SunnyShah :那么从 URL 下载您的视频的另一种选择是什么?
  • 转到我的答案@moni_BQ
  • 希望对您有所帮助:stackoverflow.com/questions/9060153/…

标签: ios video download youtube mpmovieplayercontroller


【解决方案1】:

由于您在 url 中提供的链接是针对 youtube 的,因此不会下载或保存在本地

在 youtube 上使用它

我使用了这个项目中的类: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);
    }
}];

【讨论】:

  • 我收到一个错误Failed extracting video URL using block due to error:Error Domain=LBYouTubeExtractorErrorDomain Code=2 "Couldn't find the stream URL." UserInfo={NSLocalizedDescription=Couldn't find the stream URL.}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-23
  • 1970-01-01
  • 2016-10-28
  • 2012-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多