iosobjective-cmpmediaquery

Getting only null returns from MPMediaQuery albums query


Ok. So, I've been running this on my iphone. It works for songs just fine, I'm able to display a list of songs complete with artist names and album covers in a UITableview, however the moment I try to search for albums I just get null. Here is my code for the album query.

    NSMutableArray *AlbumsList = [[NSMutableArray alloc] init];;
    MPMediaQuery *allAlbums = [[MPMediaQuery alloc] init];
    NSArray *itemsFromAlbumQuery = [allAlbums collections];
    //NSLog(@"Albums: %@", itemsFromAlbumQuery);
    NSString* mediaTitle; //the title holder
    int whileX = 0;
    MPMediaItem* mediaItem; //the media holding object
    while(whileX != [itemsFromAlbumQuery count]){
        mediaItem = itemsFromAlbumQuery[whileX];
        mediaTitle = [mediaItem valueForProperty:MPMediaItemPropertyAlbumTitle];
            [AlbumsList addObject: mediaTitle];

        //create a list of song names

        whileX += 1;



    }

I've tried doing a specific albums query rather than general query and had the same result, I'm a little confused here, if I get this query below to return [allAlbums items] with mediaTitle set to Album name I get the album name for every song in my library, however if I try to get collections I just get null, all my songs are defined correctly with the album and artist fields filled in so I know this isn't the issue.


Solution

  • This works for me

    MPMediaQuery *allAlbums = [MPMediaQuery albumsQuery];
    NSArray *itemsFromAlbumQuery = [allAlbums collections];
    
            for(int i=0; i< itemsFromAlbumQuery.count;i++){
                MPMediaItemCollection* mc=itemsFromAlbumQuery[i];
                MPMediaItem* item=mc.representativeItem;
                NSString* title=[item valueForProperty:MPMediaItemPropertyAlbumTitle];
    
            }