获取iPod库中的项目专辑年份?

时间:2011-02-01 12:36:38

标签: iphone ios ipod

尝试以下代码:

// 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);
}

在所有情况下,我对NSDates都没有... 但在iPod库中我可以看到日期,因此必须提供信息。 获得它的正确方法是什么?

1 个答案:

答案 0 :(得分:19)

嗯,我想我已经明白了。我以为iTunes中的'Year'列对应于API中的MPMediaItemPropertyReleaseDate - 但这是错误的。我的物品实际上没有发布日期信息。

我还找到了如何获取'年份'信息(我需要的信息),但不幸的是没有记录:

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