123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- //
- // audioPlayListManager.m
- // 双子星云手机
- //
- // Created by xd h on 2024/7/15.
- //
- #import "audioPlayListManager.h"
- #import <MJExtension.h>
- @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<lastDataArr.count; i++) {
- NSDictionary *dict = lastDataArr[i];
- lastFileModel *model = [lastFileModel mj_objectWithKeyValues:dict];
- if(model){
- [dataArr addObject:model];
- }
- }
-
- //数组按第一次预览时间倒序
- if(dataArr.count >= 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
|