How to retrieve iTunes preferences programmatically to find audio libraries?

自闭症网瘾萝莉.ら 提交于 2019-12-20 15:27:33

问题


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 directory path(s) from iTunes. How would I retrieve the directory paths(s) from iTunes? Would this be in some sort of preference setting?

Thanks.


回答1:


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



回答2:


Edit: As others have pointed out, the approach below doesn't address the case that the iTunes library may not be in a user's home folder. See David's answer for a better solution. Finding the user's music folder may still be somewhat useful for finding a standard (though not the only) location of non-iTunes audio files (e.g. as the default folder for an open panel).

Original answer:

You could locate the user's Music folder via

NSString *musicPath = [NSSearchPathForDirectoriesInDomains(NSMusicDirectory, NSUserDomainMask, YES) objectAtIndex:0];

This will normally have an iTunes subdirectory that contains the file "iTunes Music Library.xml" which contains most metadata of the user's iTunes library. Don't try to write to it though - iTunes only creates this file so that other apps can access it, but doesn't use it internally otherwise.




回答3:


Use the Scripting Bridge to talk to iTunes. You can ask iTunes about the user's library and every track in it; file tracks will freely tell you where they are in the file-system.

Alternatively, embed Karelia Software's iMedia Browser framework, which will talk to iTunes for you.

You probably should also be able to open audio files that the user drops on your app or assigns to it in Finder's Get Info and opens. Consider making your application document-based; alternatively, implement the right application delegate method and declare the right Info.plist properties.



来源:https://stackoverflow.com/questions/4703945/how-to-retrieve-itunes-preferences-programmatically-to-find-audio-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!