tags:

views:

40

answers:

3

Hi there,

does any of you have example code (or a link to it) of how to retrieve all music albums or artist from the iPod media library?

Thanks in advance!

+1  A: 

There are plenty of examples in the iTunes SDK, which you'll need if you want to do this anyway.

Dave Markle
+1  A: 

Use a MKMediaQuery:

MPMediaQuery *allAlbumsQuery = [MPMediaQuery albumsQuery];
NSArray *allAlbumsArray = [allQuery collection];

The allItems array does now contain MPMediaItemCollections, grouping is done by album. Now you can walk through the arrays.

for (MPMediaItemCollection *collection in allAlbumsArray) {
    MPMediaItem *item = [collection representativeItem];
}
muffix
A: 

Thanks for the answer, here is working sample code that prints out the albums and artists in case someone needs it:

NSMutableString *outText = [[NSMutableString alloc] initWithString:@"Albums:"];
[outText appendFormat:@"\r\n count:%i",[[[MPMediaQuery albumsQuery] collections] count]];
for (MPMediaItemCollection *collection in [[MPMediaQuery albumsQuery] collections]) {
        [outText appendFormat:@"\r\n -%@",[[collection representativeItem] valueForProperty:MPMediaItemPropertyAlbumTitle]];
}

[outText appendString:@"\r\n\r\n Artist:"];

for (MPMediaItemCollection *collection in [[MPMediaQuery artistsQuery] collections]) {
        [outText appendFormat:@"\r\n -%@",[[collection representativeItem] valueForProperty:MPMediaItemPropertyArtist]];
}
NSLog(@"%@",[outText autorelease]);
dkk