I'm currently using the below code in one of my applications.
It works great on a device running 3.0 but now always returns an Album count of 0 for each Playlist if the device is 3.1.
Please let me know if I'm doing this incorrectly. I'm basically try to loop through the Playlists and count the number of Albums and Tracks for each one in order to then show this data on screen.
I have also raised a Bug Report with Apple : 7242168
Can someone please take a look?
- (void)loadDevicePlaylistData {
[playlistArray removeAllObjects];
MPMediaLibrary *library = [MPMediaLibrary defaultMediaLibrary];
if (library) {
// This queries everything and creates a false Playlist called "All Music"
[self queryMusicAndSaveWithPlaylistName:@"All Music" PredicateValue:@""];
MPMediaQuery *playlistQuery = [MPMediaQuery playlistsQuery];
for(MPMediaItemCollection *playlistCollection in [playlistQuery collections]) {
MPMediaPlaylist *playlist = (MPMediaPlaylist *)playlistCollection;
// Create predicate based on loop through Playlists
NSString *playlistName = [NSString stringWithFormat:@"%@",[playlist valueForProperty:MPMediaPlaylistPropertyName]];
// Query data for specific Playlist
[self queryMusicAndSaveWithPlaylistName:playlistName PredicateValue:playlistName];
}
}
}
- (void)queryMusicAndSaveWithPlaylistName:(NSString *)playlistName PredicateValue:(NSString *)predicateValue {
MPMediaPropertyPredicate *playlistPredicate = [MPMediaPropertyPredicate predicateWithValue:predicateValue forProperty:MPMediaPlaylistPropertyName comparisonType:MPMediaPredicateComparisonEqualTo];
// Check how many albums in the Query
int playlistAlbumCountTemp = 0;
MPMediaQuery *albumQuery = [MPMediaQuery albumsQuery];
[albumQuery addFilterPredicate:playlistPredicate];
// Count the number of albums in current Playlist
for(MPMediaItemCollection *albumCollection in [albumQuery collections]) playlistAlbumCountTemp++;
// Number of Albums in current Playlist
NSNumber *playlistAlbumCount = [NSNumber numberWithInt:playlistAlbumCountTemp];
NSArray *playlistItems = [albumQuery items];
NSNumber *playlistTrackCount = [NSNumber numberWithInt:[playlistItems count]];
[playlistArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
playlistName, @"PL Name",
playlistAlbumCount, @"PL Albums",
playlistTrackCount, @"PL Tracks",
nil]];
}