Retrieve list of songs ordered by last play time in iOS

风格不统一 提交于 2019-12-01 04:05:00

One way is to take the array of MPMediaItems you get from the MPMediaQuery and sort it by MPMediaItemPropertyLastPlayedDate using an NSSortDescriptor:

NSTimeInterval start  = [[NSDate date] timeIntervalSince1970];

MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
NSArray *songsArray = [songsQuery items];

NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:MPMediaItemPropertyLastPlayedDate
                                                         ascending:NO];
NSArray *sortedSongsArray = [songsArray sortedArrayUsingDescriptors:@[sorter]];

NSTimeInterval finish = [[NSDate date] timeIntervalSince1970];
NSLog(@"Execution took %f seconds.", finish - start);

This sorts the new array by most recently played first. I tested this on a iPhone 4S using 2000 songs and it took .98 seconds.

I think MPMediaQuery is the only way to get recently played songs from an iOS device at this time.

You can use property MPMediaItemPropertyLastPlayedDatewhich will return you the most recent calendar date and time on which the user played the media item. Value is an NSDate object.

http://developer.apple.com/library/IOs/#documentation/MediaPlayer/Reference/MPMediaItem_ClassReference/Reference/Reference.html#//apple_ref/doc/constant_group/General_Media_Item_Property_Keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!