Get album year for item in iPod library?

ぐ巨炮叔叔 提交于 2019-12-18 03:43:32

问题


Trying the following code:

// Per albums
MPMediaQuery *albumsQuery = [MPMediaQuery albumsQuery];
NSArray *collections = [albumsQuery collections];

for (MPMediaItemCollection *collection in collections)
{
    NSDate *collectionReleaseDate = [collection valueForProperty: MPMediaItemPropertyReleaseDate];
    NSLog(@"collection release date: %@", collectionReleaseDate);

    MPMediaItem *representativeItem = [collection representativeItem];
    NSDate *representativeItemReleaseDate = [representativeItem valueForProperty: MPMediaItemPropertyReleaseDate];
    NSLog(@"representativeItem release date: %@", representativeItemReleaseDate);
}

// Just per item
MPMediaQuery *query = [[MPMediaQuery alloc] init];
NSArray *items = [query items];

for (MPMediaItem *item in items)
{
    NSDate *date = [item valueForProperty: MPMediaItemPropertyReleaseDate];
    NSLog(@"release date: %@", date);
}

In all cases I get nil's for NSDates... But in the iPod library I can see dates, so the information must be available. What is the correct way to obtain it?


回答1:


Well, I think I've figured it out. I was thinking that 'Year' column in iTunes corresponds to MPMediaItemPropertyReleaseDate in API - but it's wrong. My items actually weren't having release date info.

I also found how to obtain 'Year' information (which I needed), but unfortunately in undocumented way:

MPMediaItem *item = ...;
NSNumber *yearNumber = [item valueForProperty:@"year"];
if (yearNumber && [yearNumber isKindOfClass:[NSNumber class]])
{
    int year = [yearNumber intValue];
    if (year != 0)
    {
        // do something with year
    }
}


来源:https://stackoverflow.com/questions/4862646/get-album-year-for-item-in-ipod-library

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