How to retrieve iTunes preferences programmatically to find audio libraries?

后端 未结 3 1476
一生所求
一生所求 2021-02-06 17:56

I\'m creating an application on the Mac in Cocoa that plays audio. I think the best place for my application to search for audio files on a user\'s computer is to get the direct

3条回答
  •  萌比男神i
    2021-02-06 18:10

    Just for those who will encounter this problem in the future, I've documented the approach that I used below.

    I decided not to use the Karelia framework because I don't need all the bells and whistles. I just need to locate the iTunes library and display a list of songs.

    I went through the Karelia framework and it appears that they locate the iTunes library via the user defaults as shown below.

    + (NSArray*) parserInstancesForMediaType:(NSString*)inMediaType
    {
        NSMutableArray* parserInstances = [NSMutableArray array];
    
        if ([self isInstalled])
        {
            CFArrayRef recentLibraries = CFPreferencesCopyAppValue((CFStringRef)@"iTunesRecentDatabases",(CFStringRef)@"com.apple.iApps");
            NSArray* libraries = (NSArray*)recentLibraries;
    
            for (NSString* library in libraries)
            {
                NSURL* url = [NSURL URLWithString:library];
                NSString* path = [url path];
                BOOL changed;
                (void) [[NSFileManager imb_threadSafeManager] imb_fileExistsAtPath:&path wasChanged:&changed];
    
                NSString *libraryPath = [path stringByDeletingLastPathComponent];   // folder containing .xml file
                [IMBConfig registerLibraryPath:libraryPath];
    
                IMBiTunesParser* parser = [[[self class] alloc] initWithMediaType:inMediaType];
                parser.mediaSource = path;
                parser.shouldDisplayLibraryName = libraries.count > 1;
                [parserInstances addObject:parser];
                [parser release];
            }
    
            if (recentLibraries) CFRelease(recentLibraries);
        }
    
        return parserInstances;
    }
    

    Using the above approach, you can locate all iTunes libraries with the following code.

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *userPref = [userDefaults persistentDomainForName:@"com.apple.iApps"];
    
    NSArray *recentDatabases = [userPref objectForKey:@"iTunesRecentDatabases"];
    
    for (NSString *key in recentDatabases)
    {
        NSLog(@"%@", key);
    }
    

    Also if you inspect the user defaults iApps dictionary you can use these other keys to get info for other applications

    iMovie
    iPhotoAutoImportPath
    iPhotoRecentDatabases
    iTunesRecentDatabases
    iTunesRecentDatabasePaths
    

提交回复
热议问题