繁体   English   中英

如何在可可中获取所有包含音乐(mp3)的文件夹路径?

[英]How to get all folders paths that contains music (mp3) in Cocoa?

我是可可的新手。我正在尝试在计算机中扫描我的应用程序中的音乐文件夹。 但是我对此有一些逻辑。 我需要从/ Users开始扫描的内容,例如,如果我具有/ Users / music / anotherMusic和/ Users / music / music2 / music3,那么我需要能够仅获取包含音乐的文件夹的顶部(/ Users /音乐)。 并且还需要继续迭代子文件夹,以便在根文件夹(/ Users / music)中拥有音乐文件的数量。 依此类推,以同样的逻辑继续扫描整个计算机。结果应显示在表格中,例如RESULTS应该像这样

--/用户/ myComputer / iTunes-77个文件夹,500个音乐文件

-/ Users / myComputer / Documents / Music -15个音乐文件夹,400个音乐文件

-/ Users / myComputer / Downloads / Media -20个音乐文件夹,325个音乐
文件(带有音乐的所有子目录的数量)

我怎么能这样做? 我所做的是在下面的代码中。 但是它一直深入到每一个包含.mp3音乐的路径,但是我需要如上所述的逻辑。

NSFileManager *manager = [NSFileManager defaultManager];

       NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:@"/Users/"];                  
        NSString * file;
      while((file=[direnum nextObject]))
      {
          NSString * FullPath=[NSString stringWithFormat:@"/Users/%@",file];

          if ([file hasSuffix:@".mp3"])
        { 
          ///gives a music file path that contains mp3, or I can cut last part, and get the folder path. But no logic as should be.
        }
     }

任何想法,或帮助表示赞赏。 谢谢。

如果我错了,请纠正我。 您想获取文件夹中mp3的数量,但是尽管一个文件夹仅包含文件夹和mp3文件,但mp3-count用于其父文件夹(如果父文件夹本身仅包含文件夹和mp3,则该文件将以父母等)对吗?

[manager enumeratorAtPath]非常有用,但是在这种情况下,肯定会需要您维护一个堆栈来跟踪浏览的文件。

递归很幸福。

- (BOOL)isMp3File:(NSString*)file
{
    return [file hasSuffix:@".mp3"];
}

- (BOOL)isHiddenFile:(NSString*)file
{
    return [file hasPrefix:@"."];
}

- (void)parseForMp3:(NSMutableDictionary*)dic
             inPath:(NSString*)currentPath
          forFolder:(NSString*)folder
{
    BOOL comboBreak = false;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError* error;
    NSArray* files = [fileManager contentsOfDirectoryAtPath:currentPath error:&error];
    if (error)
    {
        //throw error or anything
    }
    for (NSString *file in files)
    {
        BOOL isDirectory = false;
        NSString *fullPath = [NSString stringWithFormat:@"%@/%@", currentPath, file];
        [fileManager fileExistsAtPath:fullPath isDirectory:&isDirectory];
        comboBreak = comboBreak || (!isDirectory &&
                                    ![self isMp3File:file] &&
                                    ![self isHiddenFile:file]);
        if (comboBreak)
            break;
    }
    for (NSString *file in files)
    {
        BOOL isDirectory = false;
        NSString *fullPath = [NSString stringWithFormat:@"%@/%@", currentPath, file];
        [fileManager fileExistsAtPath:fullPath isDirectory:&isDirectory];
        if (isDirectory)
        {
            if (!comboBreak)
            {
                [self parseForMp3:dic inPath:fullPath forFolder:folder];
            }
            else
            {
                [self parseForMp3:dic inPath:fullPath forFolder:fullPath];
            }
        }
        else if ([self isMp3File:file])
        {
            NSNumber *oldValue = [dic valueForKey:folder];
            oldValue = [NSNumber numberWithUnsignedInteger:[oldValue unsignedIntegerValue] + 1];
            [dic setValue:oldValue forKey:folder];
        }
    }
}


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [self parseForMp3:dic inPath:@"/Users/panosbaroudjian" forFolder:@"/Users/panosbaroudjian"];
    NSLog(@"%@", dic);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM