下载的视频 - AFHTTPRequestOperation与NSURLSessionDownloadTask

时间:2014-06-30 10:38:37

标签: io afnetworking-2

我尝试更新现有的下载模型,因此我已经替换了旧代码:

AFHTTPRequestOperation *downloadRequest = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[downloadRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSData *data = [[NSData alloc] initWithData:responseObject];
    [data writeToFile:video2Save.localFilePath atomically:YES];

    video2Save.downloadComplete = YES;
    [YEPersistentModelHelper saveData:_downloadVideos ToDiskWithIdentifier:persistentIdDownloadedVideos];

    NSLog(@"file downloading complete : %@", video2Save.localFilePath);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"file downloading error : %@", [error localizedDescription]);
}];
[downloadRequest start];*/

以下内容:

NSURLSessionDownloadTask *downloadTask = [_sessionManager downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    return [documentsDirectoryURL URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",video2Save.videoVersionId]];

} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    NSLog(@"File downloaded to: %@", filePath);
    video2Save.localFilePath  = [[filePath filePathURL] absoluteString];
    video2Save.downloadComplete = YES;
    [YEPersistentModelHelper saveData:_downloadVideos ToDiskWithIdentifier:persistentIdDownloadedVideos];


    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *err = nil;
    NSDictionary *att = [fileManager attributesOfItemAtPath:video2Save.localFilePath error:&err];
    NSLog(@"NSDictionary: %@", att);

}];
[downloadTask resume];

似乎工作正常。完整块被执行&该文件存在于跟踪目标。

问题是,我不再可以播放视频了!我使用MPMoviePlayerController抛出这个有用的错误:

_itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0; }

唯一的区别似乎是文件权限。第一个增加了一个" staff" -group&每个人都可以阅读,而第二个只允许访问#34; me"。但即使我在发现者中改变它,我也无法播放它......

有没有人有想法!?

1 个答案:

答案 0 :(得分:0)

保存位置文件使用路径no absoluteString

video2Save.localFilePath = [[filePath filePathURL] absoluteString];

即使要播放也不要调用absoluteString ..只需使用路径即可 像这样调用视频

  NSURL *FilePathURL = [NSURL fileURLWithPath:[docDir stringByAppendingPathComponent:fileToCheck]];
  [[myvideoCalss :[FilePathURL path]]