原因:返回的视频url经过url编码已经含有%,无需在经过url转码
https://xxxx.oss-cn-xxx.aliyuncs.com/video/%E9%A5%BF%E4%BA%86%E9%A5%BF%E4%BA%86.mp4
stringByAddingPercentEscapesUsingEncoding不用写.
-(void)clickDownLoad{
NSLog(@"clickDownLoad videoUrl :%@",_movie.videoUrl);
if (_movie.videoUrl) {
}else{
[RemindView showHUDWithText:@"下载地址错误" delay:1 onView:KeyWindow];
return;
}
[self showHud];
NSString *urlstr = [NSString stringWithFormat:@"%@",_movie.videoUrl];
//https://yxhuda.oss-cn-shenzhen.aliyuncs.com/video/%E9%A5%BF%E4%BA%86%E9%A5%BF%E4%BA%86.mp4
//这样的特殊字符无法播放或者下载,先url转码
// urlstr = [urlstr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSLog(@"clickDownLoad newurl :%@",urlstr);
// urlstr = [urlstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"clickDownLoad newUtf8Url :%@",urlstr);
NSURL *url = [NSURL URLWithString:urlstr];
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *dateStr = [[ZYPHelper shareHelper] dateToString:[NSDate date] withDateFormat:@"YYYYMMDDHHmmSS"];
NSString *savePath = [cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",dateStr]];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"video/mpeg",@"video/mp4",@"audio/mp3",nil];//可下载@"text/json", @"text/javascript",@"text/html",@"video/mpeg",@"video/mp4",@"audio/mp3"等
NSString *fullPath = savePath;//要保存的沙盒路径
NSURLRequest *request1 = [NSURLRequest requestWithURL:url];//在线路径
NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request1 progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"downloadProgress - :%f",downloadProgress.completedUnitCount/downloadProgress.totalUnitCount*0.01);
} destination:^NSURL *(NSURL *targetPath,NSURLResponse *response) {
NSString *path_sandox =NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES)[0];
NSLog(@"path_sandox:%@",path_sandox);
NSString *path = [path_sandox stringByAppendingPathComponent:response.suggestedFilename];
NSLog(@"path:%@",path);
return [NSURL fileURLWithPath:fullPath];
} completionHandler:^(NSURLResponse *_Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
[self hideHud];
if(error){
[RemindView showHUDWithText:@"下载失败" delay:1 onView:KeyWindow];
}else{
// [self showHud]; UISaveVideoAtPathToSavedPhotosAlbum(savePath, self, @selector(), (__bridge void *)self);
UISaveVideoAtPathToSavedPhotosAlbum(savePath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
// [RemindView showHUDWithText:@"下载完成" delay:1 onView:KeyWindow];
}
}];
[task resume];
}