2013-08-24 56 views
1

我想要获取用户iPhone库中所有视频项目的列表,并按照专辑名称先排序,然后按轨道编号排序这将把所有电视节目和播客放入适当的节目/剧集顺序中)。事情是,我无法找出一种方法来排序MPMediaItems这样的两个参数。查询MPMediaItems按专辑标题和曲目号排序

现在,我这样设置查询:

MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:[NSNumber numberWithInteger:MPMediaTypeAnyVideo] forProperty:MPMediaItemPropertyMediaType]; 

MPMediaQuery *query = [[MPMediaQuery alloc] init]; 
[query addFilterPredicate:predicate]; 

NSArray *arrayMediaItems = [[NSArray alloc] init]; 
arrayMediaItems = [query items]; 

,然后我喜欢这个排序是:

NSArray *sortedArray = [arrayMediaItems sortedArrayUsingComparator:^(id a, id b) { 
    NSNumber *first = [(MPMediaItem*)a valueForProperty:MPMediaItemPropertyAlbumTitle]; 
    NSNumber *second = [(MPMediaItem*)b valueForProperty:MPMediaItemPropertyAlbumTitle]; 
    return [first compare:second]; 
}]; 

事情是,它返回一个列表,其中所有的相册(读:显示)是按照正确的顺序排列的,但是其中的情节只是按字母顺序排列的,这显然是不正确的。我试着这样做:

NSSortDescriptor *sortAlbum = [[NSSortDescriptor alloc] initWithKey:@"MPMediaItemPropertyAlbumTitle" ascending:YES]; 
NSSortDescriptor *sortTrackNum = [[NSSortDescriptor alloc] initWithKey:@"MPMediaItemPropertyAlbumTrackNumber" ascending:YES]; 
NSArray *sortDescriptors = @[sortAlbum, sortTrackNum]; 
sortedArray = [arrayMediaItems sortedArrayUsingDescriptors:sortDescriptors]; 

但我得到一个错误:this class is not key value coding-compliant for the key MPMediaItemPropertyAlbumTitle。据我了解,这是因为我试图按属性而不是键进行排序。

所以我的问题是这样的:如何排序MPMediaItems多种因素?我可以订购初始结果(例如按照音轨号码而不是按标题字母顺序排列),然后按照专辑名称排序?有两种方式使用NSSortDescriptorsortedArrayUsingComparator吗?

这样做的最好方法是什么?

回答

0

我结束了从this question找到一个代码示例。我的工作量远远少于1,200件,因此无论是问题中的代码还是答案中的代码都适用于我,但我使用了效率方面的答案。 (它还除了专辑标题,我喜欢作为对专辑的支票使用MPMediaItemPropertyAlbumPersistentID /显示了相同的标题。)

sortedArray = [arrayMediaItems sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { 
    NSComparisonResult compareResult; 

    NSString *first1 = [(MPMediaItem*)a valueForProperty:MPMediaItemPropertyAlbumTitle]; 
    if(first1 == nil) first1 = @" "; // critical because compare will match nil to anything and result in NSOrderedSame 
    NSString *second1 = [(MPMediaItem*)b valueForProperty:MPMediaItemPropertyAlbumTitle]; 
    if(second1 == nil) second1 = @" "; // critical because compare will match nil to anything and result in NSOrderedSame 
    compareResult = [first1 compare:second1]; 

    if (compareResult == NSOrderedSame) { 
     NSString *first2 = [(MPMediaItem*)a valueForProperty:MPMediaItemPropertyAlbumPersistentID]; 
     NSString *second2 = [(MPMediaItem*)b valueForProperty:MPMediaItemPropertyAlbumPersistentID]; 
     compareResult = [first2 compare:second2]; 
     if(compareResult == NSOrderedSame) { 
      NSString *first3 = [(MPMediaItem*)a valueForProperty:MPMediaItemPropertyAlbumTrackNumber]; 
      NSString *second3 = [(MPMediaItem*)b valueForProperty:MPMediaItemPropertyAlbumTrackNumber]; 
      compareResult = [first3 compare:second3]; 
     } 
    } 
    return compareResult; 
}]; 

我希望这可以帮助别人!

1

将此视为评论。因为我无法在那里发布图片。

enter image description here

enter image description here

上述图像是用于MediaPlayer框架常量截图。你说有没有财产喜欢“MPMediaItemPropertyAlbumTitle”和“MPMediaItemPropertyAlbumTrackNumber”。因为它们是一些其他属性的常量。

尝试albumTitlealbumTrackNumber,而不是 “MPMediaItemPropertyAlbumTitle” 和 “MPMediaItemPropertyAlbumTrackNumber

+0

按照'albumTitle'单独排序的单词,但按'albumTrackNumber'排序崩溃,错误为:终止应用程序,由于未捕获的异常'NSUnknownKeyException',原因:'[ valueForUndefinedKey:]:该类不是键符合关键字TrackNumber.''的值编码 – Nerrolken