Question

I have an "songs" NSMutableArray filled with all songs on the device:

    MPMediaQuery *everything = [MPMediaQuery songsQuery];   
    NSArray *songCollections = [everything collections];

    songs = [NSMutableArray array];
    for (MPMediaItemCollection *song in songCollections) {
        MPMediaItem *representativeItem = [song representativeItem];  
        [songs addObject:representativeItem];
    }

Is there a way to sort them by album? I have noticed that adding

   [everything setGroupingType:MPMediaGroupingAlbum]; 

produces a strange result (it's just pushing in the array the first song for each album)

Thanks in advance

Était-ce utile?

La solution

You can fetch the albums and then iterate through them to populate a song array, resulting in the songs sorted with respect to the albums:

NSMutableArray *sortedSongs = [[NSMutableArray alloc] init];
MPMediaQuery *albums = [MPMediaQuery albumsQuery];
NSArray* albumObjects = [albums collections];
for(MPMediaItemCollection *album in albumObjects) {
    for(MPMediaItem *song in album.items) {
        [sortedSongs addObject:song];
    }
}

NSLog(@"sorted songs : %@", sortedSongs);

Hope this helps

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top