I am currently trying to categorise a users music collection and the ability to get the users most played songs/artists would greatly improve the user experience in the app. Is it possible to get the play count? and how would I go about doing it
MPMediaItem has the method:
- (id) valueForProperty: (NSString *) property
This method return the media property key that you want the corresponding value of. And one of the possibles values is MPMediaItemPropertyPlayCount:
The number of times the user has played the media item. Value is an NSNumber object representing an NSUInteger data type.
You can check the doc here and here.
Should you want to find the user's most played track(s), you could:
Store all media items (i.e. [[MPMediaQuery songsQuery] items]
) and their properties in a database (i.e. Core Data), fetch them with an NSFetchRequest and sort the results with NSSortDescriptor.
...or use [[MPMediaQuery songsQuery] items]
and sort the results on the MPMediaItemPropertyPlayCount
property.
Option (1) is probably best, especially if you're looking to categorize the music collection (I guess (2) could be worse performance-wise too).
There's also a similar answer on SO to help answer your question.
Check out the Apple docs for more info on MPMediaQueries.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With