2013-08-24 32 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