// // audioPlayListManager.m // 双子星云手机 // // Created by xd h on 2024/7/15. // #import "audioPlayListManager.h" #import @implementation audioPlayListManager + (instancetype)shareManager { static audioPlayListManager *_instance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance; } - (instancetype)init { if (self = [super init]) { } return self; } - (NSString*)uid{ if(!_uid || _uid.length == 0){ return @"userName"; } return _uid; } - (NSString *)getFullDirector { NSString *account = self.uid; if (account.length != 0) { NSString *fileFolder = [HWDataManager documentPathForAccount:account fileFolder:KaudioPlayListDirector]; // 创建文件储存路径 if (![[NSFileManager defaultManager] fileExistsAtPath:fileFolder]) { [[NSFileManager defaultManager] createDirectoryAtPath:fileFolder withIntermediateDirectories:YES attributes:nil error:nil]; } return fileFolder; }else { HLog(@"创建文件失败!"); return @""; } } #pragma mark- last file plist Path - (NSString *)getAudioPlayListPlistPath { NSString *fileFolder = [self getFullDirector]; return [fileFolder stringByAppendingPathComponent:@"audioPlayList.plist"];; } #pragma mark- last file plist - (NSMutableDictionary *)audioPlayPList { if (!_audioPlayPList) { // 内存没有 _audioPlayPList = [[NSDictionary dictionaryWithContentsOfFile:[self getAudioPlayListPlistPath]] mutableCopy]; // 本地加载 if (!_audioPlayPList) { // 本地没有,分配内存 _audioPlayPList = [NSMutableDictionary dictionary]; } } return _audioPlayPList; } - (NSMutableArray*)audioPlayListArr{ if (!_audioPlayListArr) { // 内存没有 NSMutableArray *dataArr = [NSMutableArray new]; if (self.audioPlayPList) { if([[self.audioPlayPList allKeys] containsObject:KaudioPlayListkey]){ NSArray *lastDataArr = self.audioPlayPList[KaudioPlayListkey]; for (int i=0; i= 2){ NSArray *sortArr = [dataArr sortedArrayUsingComparator:^NSComparisonResult(lastFileModel* obj1, lastFileModel* obj2) { NSTimeInterval time1 = obj1.lastPreTime; NSTimeInterval time2 = obj2.lastPreTime; if (time1 < time2) { return NSOrderedDescending; } if (time1 > time2) { return NSOrderedAscending; } return NSOrderedSame; }]; dataArr = [NSMutableArray arrayWithArray:sortArr]; } } } _audioPlayListArr = dataArr; } return _audioPlayListArr; } /** 增加配置信息 */ - (BOOL)saveFileInfoWith:(lastFileModel *)lastFileMod with:(NSString*)fullPath { if(!lastFileMod || !fullPath){ return NO; } BOOL flag = NO; @synchronized (self) { //NSString *key = fullPath; //1.本地持久化 NSMutableDictionary *dictM = self.audioPlayPList; NSMutableArray *dataArr = self.audioPlayListArr; BOOL isInPlayList = NO; NSMutableArray *saveDictArr = [NSMutableArray new]; for (lastFileModel*model in dataArr) { if([model.path isEqualToString:fullPath]){ isInPlayList = YES; break; } //把原来的数据加到saveDictArr //lastFileMod.localPath = [self getAudioCacheFullPathBy:model.name]; NSDictionary *dict = [model lastFileInfoFun]; [saveDictArr addObject:dict]; } if(isInPlayList){ HLog(@"音乐保存在播放列表里面了") return NO; } //1.写到内存 //[dataArr addObject:lastFileMod]; [dataArr insertObject:lastFileMod atIndex:0]; //2.本地持久化 把新的的数据加到saveDictArr lastFileMod.lastPreTime = [iTools getNowTimeStamp]; lastFileMod.localPath = [self getAudioCacheFullPathBy:lastFileMod.name]; NSDictionary *dict = [lastFileMod lastFileInfoFun]; [saveDictArr addObject:dict]; if(saveDictArr){ [dictM setValue:saveDictArr forKey:KaudioPlayListkey]; } NSString * PlistPath = [self getAudioPlayListPlistPath]; flag = [dictM writeToFile:PlistPath atomically:YES]; } return flag; } /** 删除配置信息 */ - (BOOL)deleteFileInfoWithUrl:(NSString *)fullPath { if(!fullPath){ return NO; } BOOL flag = NO; @synchronized (self) { //1.内存删除 for (lastFileModel*preModel in self.audioPlayListArr) { if([preModel.path isEqualToString:fullPath]){ [self.audioPlayListArr removeObject:preModel]; break; } } //2.本地持久化删除 NSMutableArray *saveDictArr = [NSMutableArray new]; for (lastFileModel*model in self.audioPlayListArr) { NSDictionary *dict = [model lastFileInfoFun]; [saveDictArr addObject:dict]; } [self.audioPlayPList setValue:saveDictArr forKey:KaudioPlayListkey]; flag = [self.audioPlayPList writeToFile:[self getAudioPlayListPlistPath] atomically:YES]; } return flag; } - (NSString*)getAudioCacheFullPathBy:(NSString*)name { NSString*fullPathDirector = [NSString stringWithFormat:@"%@/%@",DocumentPath,@"playAudioCache"]; // 创建文件储存路径 if (![[NSFileManager defaultManager] fileExistsAtPath:fullPathDirector]) { [[NSFileManager defaultManager] createDirectoryAtPath:fullPathDirector withIntermediateDirectories:YES attributes:nil error:nil]; } NSString*fullPath = [NSString stringWithFormat:@"%@/%@",fullPathDirector,name]; return fullPath; } //df准备播放是下载 - (void)beginToDownloadByUrl:(NSString*)url { if(!url)return; //解码 //NSString * urlString = [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *urlFileName = [url lastPathComponent]; NSString *filePath = [iTools base64UrlDecoder:urlFileName]; //NSString * fileName = [filePath lastPathComponent]; NSString *urlStr = ksharedAppDelegate.NASFileByBoxService; // NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFiles/%@",urlStr,urlFileName]; NSString *fileUrl = [[NSString alloc] initWithFormat:@"%@getFile?path=%@",urlStr,filePath]; fileUrl = [fileUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; if (![self checkDownloadDoneBy:filePath]) { [[audioPlayDownloadManager shareManager] addDownloadWithURLs:@[fileUrl]]; } } //检测是否下载完成 - (BOOL)checkDownloadDoneBy:(NSString*)filePath { lastFileModel*preDownloadModel = nil; for (lastFileModel*model in self.audioPlayListArr) { if([model.path isEqualToString:filePath]){ preDownloadModel = model; break; } } if(!preDownloadModel){ return NO; } //NSString *curFilePath = preDownloadModel.localPath; NSString *curFilePath = [[audioPlayListManager shareManager] getAudioCacheFullPathBy:preDownloadModel.name]; // 创建NSFileManager实例 NSFileManager *fileManager = [NSFileManager defaultManager]; // 使用fileExistsAtPath方法检查文件是否存在 BOOL fileExists = [fileManager fileExistsAtPath:curFilePath]; // 再加一层判断看看 if(!fileExists){ NSDictionary *attributes = [fileManager attributesOfItemAtPath:curFilePath error:nil]; if (attributes) { fileExists = YES;; } else { HLog(@"文件不存在:%@",filePath); } } if(preDownloadModel && preDownloadModel.isDownDoneType && fileExists){ return YES; } return NO; } /** 更新下载完成 */ - (void)changeFileToDonewith:(NSString*)url { NSString * decodeUrl= [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSArray *urlArr = [decodeUrl componentsSeparatedByString:@"?path="]; if(urlArr.count >= 2){ NSString *filePath = urlArr.lastObject; for (lastFileModel*model in self.audioPlayListArr) { if([model.path isEqualToString:filePath]){ model.isDownDoneType = YES; [self updateLoaclInfoFun]; break; } } } } //本地更新数据持久化 - (void)updateLoaclInfoFun { @synchronized (self) { //1.本地持久化 NSMutableDictionary *dictM = self.audioPlayPList; NSMutableArray *dataArr = self.audioPlayListArr; NSMutableArray *saveDictArr = [NSMutableArray new]; for (lastFileModel*model in dataArr) { model.localPath = [self getAudioCacheFullPathBy:model.name]; NSDictionary *dict = [model lastFileInfoFun]; [saveDictArr addObject:dict]; } //2.本地持久化 if(saveDictArr){ [dictM setValue:saveDictArr forKey:KaudioPlayListkey]; } NSString * PlistPath = [self getAudioPlayListPlistPath]; [dictM writeToFile:PlistPath atomically:YES]; } } /** 检测是否下载完成 */ - (NSString*)checkFileToDownloadDonewith:(NSString*)url { NSString *urlFileName = [url lastPathComponent]; NSString *filePath = [iTools base64UrlDecoder:urlFileName]; for (lastFileModel*model in self.audioPlayListArr) { if([model.path isEqualToString:filePath]){ if (model.isDownDoneType) { if(model.localPath){ NSString *curFilePath = [[audioPlayListManager shareManager] getAudioCacheFullPathBy:model.name]; // 创建NSFileManager实例 NSFileManager *fileManager = [NSFileManager defaultManager]; // 使用fileExistsAtPath方法检查文件是否存在 BOOL fileExists = [fileManager fileExistsAtPath:curFilePath]; if (fileExists) { return curFilePath; } else { return @""; } } } break; } } return @""; } @end