Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get album year for item in iPod library?

Tags:

ios

iphone

ipod

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?

like image 450
ivanzoid Avatar asked Feb 01 '11 12:02

ivanzoid


1 Answers

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
    }
}
like image 65
ivanzoid Avatar answered Oct 16 '22 06:10

ivanzoid