下载视频-AFHTTPRequestOperation与NSURLSessionDownloadTask
我尝试更新现有的下载模式,因此我已替换了旧代码:下载视频-AFHTTPRequestOperation与NSURLSessionDownloadTask,io,afnetworking-2,Io,Afnetworking 2,我尝试更新现有的下载模式,因此我已替换了旧代码: AFHTTPRequestOperation *downloadRequest = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [downloadRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSData *data = [
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; }
唯一的区别似乎是文件权限。第一个添加了一个“员工”组&每个人都可以阅读,而第二个只允许“我”访问。但即使我在finder中更改它,我也无法播放它
有人有主意吗 要保存位置文件,请使用路径无绝对字符串 video2Save.localFilePath=[[filePath filePathURL]绝对字符串] 不要打电话给absoluteString,即使是为了玩。。就用这条路吧 像这样的例子来调用视频
NSURL *FilePathURL = [NSURL fileURLWithPath:[docDir stringByAppendingPathComponent:fileToCheck]];
[[myvideoCalss :[FilePathURL path]]
嘿,我想这可能触发了这个有用的错误;)