Getting the most popular track from the iPod library (MPMediaQuery)

I need to get out of the 25 most Played Songs from my iPod library with my iPhone app. I am using MPMediaQuery.

One solution would be to go through all the tracks and compare them using MPMediaItemPropertyAlbumTrackCount. But I think this is a little inefficient. Is there a way to directly get a playlist of the most popular items?

+1
source share
2 answers

I think you are looking for MPMediaItemPropertyPlayCount , not MPMediaItemPropertyAlbumTrackCount. MPMediaItemPropertyAlbumTrackCount is the track number of the song that appears in his album.

MPMediaItemPropertyPlayCount , unfortunately, cannot be used to create queries using MPMediaQuery, since it is a user-defined property.

Your best option is to store all playback counters in a database, such as Core Data, when your application opens for the first time and updates it, recording notifications when the user library changes.

+4
source

you can use NSSortDescriptor to sort the most played songs

MPMediaQuery *everything = [[MPMediaQuery alloc] init]; NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:MPMediaItemPropertyPlayCount ascending:NO]; NSArray *sortedSongsArray = [[everything items] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sorter]];

+1
source

All Articles