Retrieve list of songs ordered by last play time in iOS

前端 未结 2 573
感情败类
感情败类 2020-12-31 18:57

I need to obtain a list of the N most recently played songs from an iOS device, in order.

The only way I can imagine doing it, at the moment, is by getting

2条回答
  •  时光说笑
    2020-12-31 19:49

    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.

提交回复
热议问题