-1

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.

Jobalisk
  • 728
  • 1
  • 6
  • 17
  • `MPMediaQuery *allAlbums = [[MPMediaQuery alloc] init];` But that is not a query for albums! _Where is your albums query?_ – matt Dec 13 '18 at 11:26
  • I can switch it for albums query, it gives me a shorter list of nulls, which I geuss mean its calling a list of albums, but they are still null none the less – Jobalisk Dec 14 '18 at 00:40
  • Well I don't know what your new code looks like. My online book contains some Objective-C code that shows how to perform an albums query. http://www.apeth.com/iOSBook/ch29.html#_exploring_the_music_library – matt Dec 14 '18 at 00:44
  • thanks, I might have a look – Jobalisk Dec 14 '18 at 00:57

1 Answers1

1

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];

        }
Wasserfloh
  • 196
  • 3
  • 6
  • Thanks, took me a we while but I got there in the end, I feel a bit stupid now not thinking that it was returning a list of lists rather than actual media items. – Jobalisk Dec 14 '18 at 01:21