MTVideoDownTool.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // MTVideoDownTool.m
  3. // Hellomacau
  4. //
  5. // Created by Simon on 2020/4/30.
  6. // Copyright © 2020 Simon. All rights reserved.
  7. //
  8. #import "MTVideoDownTool.h"
  9. @implementation MTVideoDownTool
  10. + (void)downloadAudioWithUrl:(NSString *)url
  11. saveDirectoryPath:(NSString *)directoryPath
  12. fileName:(NSString *)fileName
  13. finish:(FinishBlock )finishBlock
  14. failed:(Failed)failed
  15. {
  16. NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  17. directoryPath = [paths objectAtIndex:0];
  18. NSString *mp3path = [NSString stringWithFormat:@"%@.mp3",fileName];
  19. NSString *file_path = [directoryPath stringByAppendingPathComponent:mp3path];
  20. NSFileManager *fm = [NSFileManager defaultManager];
  21. /// 判断文件是否已经存在
  22. if ([fm fileExistsAtPath:file_path]){
  23. // finishBlock(file_path);
  24. failed(1);
  25. }else{
  26. /// 不存在时
  27. NSURL *URL = [NSURL URLWithString:url];
  28. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  29. //AFN3.0+基于封住URLSession的句柄
  30. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
  31. //请求
  32. NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  33. //下载Task操作
  34. NSURLSessionDownloadTask *_downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
  35. //进度
  36. NSString *ss = [NSString stringWithFormat:@"已下载:%@",downloadProgress.localizedDescription];
  37. ShowMessage(ss);
  38. } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
  39. NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  40. NSString *path = [cachesPath stringByAppendingPathComponent:response.suggestedFilename];
  41. return [NSURL fileURLWithPath:path];
  42. } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  43. // filePath就是你下载文件的位置,你可以解压,也可以直接拿来使用
  44. NSString *armFilePath = [filePath path];// 将NSURL转成NSString
  45. //重命名
  46. NSString *nNameFilePath = [MTVideoDownTool p_setupFileRename:armFilePath andFileName:fileName];
  47. finishBlock(nNameFilePath);
  48. }];
  49. [_downloadTask resume];
  50. }
  51. }
  52. /**
  53. 对文件重命名
  54. @param filePath 旧路径
  55. @return 新路径
  56. */
  57. + (NSString *)p_setupFileRename:(NSString *)filePath andFileName:(NSString *)Name{
  58. NSString *lastPathComponent = [NSString new];
  59. //获取文件名: 视频.MP4
  60. lastPathComponent = [filePath lastPathComponent];
  61. //获取后缀:MP4
  62. NSString *pathExtension = [filePath pathExtension];
  63. //用传过来的路径创建新路径 首先去除文件名
  64. NSString *pathNew = [filePath stringByReplacingOccurrencesOfString:lastPathComponent withString:@""];
  65. //然后拼接新文件名:
  66. NSString *moveToPath = [NSString stringWithFormat:@"%@%@.%@",pathNew,Name,pathExtension];
  67. NSFileManager *fileManager = [NSFileManager defaultManager];
  68. //通过移动该文件对文件重命名
  69. BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil];
  70. if (isSuccess) {
  71. NSLog(@"rename success");
  72. }else{
  73. NSLog(@"rename fail");
  74. }
  75. return moveToPath;
  76. }
  77. @end