Search code examples
iosenumerationnsfilemanager

How to scan for audio files in iOS


My app downloads a zip file from a server and extracts the contents to the device. I need a way to find all of the audio files (mp3, mp4a, m4b, etc.) It also needs to scan all the subdirectories. Right now this is what I am doing.

-(NSArray *)audioPathsInDirectory:(NSString *)directory {
    NSDirectoryEnumerator *enumerator =  [[NSFileManager defaultManager] enumeratorAtPath:directory];
    NSMutableArray *audioPathArray = [NSMutableArray array];
    NSString *partialFilePath;
    while (partialFilePath = [enumerator nextObject]) {
        if ([[partialFilePath pathExtension] isEqualToString:@"m4b"] ||
            [[partialFilePath pathExtension] isEqualToString:@"m4a"] ||
            [[partialFilePath pathExtension] isEqualToString:@"mp3"]) {
            [audioPathArray addObject:partialFilePath];
        }
    }
    return audioPathArray;
}

My worry is that the "isEqualToString:" parts will miss some things. Is there a better way to accomplish this?


Solution

  • What do you mean by My worry is that the "isEqualToString:" parts will miss some things?

    There is nothing wrong with your approach but it is better convert extension using lowercaseString before compare to make sure it handle case sensibility.