1

私のアプリは、起動時に音楽プレイリストを自動的に読み込みます。そのために、曲の IDMPMediaItemPropertyPersistentIDをデータベースに保存し、次回アプリを起動するときに曲を読み込みます。主なコードは次のとおりです。

MPMediaQuery *MPMediaSongQuery = [MPMediaQuery songsQuery];

MPMediaPropertyPredicate *iPodMusicSongPredicateiPodMusicSongPredicate = [MPMediaPropertyPredicate 
                                  predicateWithValue:[NSNumber numberWithUnsignedLongLong: songID] 
                                  forProperty:MPMediaItemPropertyPersistentID     
                                  comparisonType:MPMediaPredicateComparisonEqualTo];

[MPMediaSongQuery addFilterPredicate:iPodMusicSongPredicate];
NSArray *collections = MPMediaSongQuery.collections;

コードは曲を 1 つずつ読み込みます。MPMediaItemPropertyPersistentID私の質問は:関数を使用するときに一度に2 つ以上の曲をクエリする方法はありますaddFilterPredicate:か? ありがとう。

4

1 に答える 1

1

複数の addFilterPredicate を使用する場合、それらは論理 AND で結合されます。したがって、最初のクエリの結果を縮小することはできますが、拡張することはできません。結果として、同じプロパティに複数の addFilterPredicates を使用することはできません。実際、結果は未定義であり、空のコレクションになる可能性が最も高いです。探しているのは、同じプロパティと論理 OR の組み合わせです。次の疑似コードに示すように、これを実現できます。

MPMediaQuery *MPMediaSongQuery = [MPMediaQuery songsQuery];

NSMutableArray *collections = [[NSMutableArray alloc] initWithCapacity:1];

for (int i=0; i < songIDCount; i++) {

   MPMediaPropertyPredicate *iPodMusicSongPredicateiPodMusicSongPredicate = [MPMediaPropertyPredicate 
                                  predicateWithValue:[NSNumber numberWithUnsignedLongLong: songID[i]] 
                                  forProperty:MPMediaItemPropertyPersistentID     
                                  comparisonType:MPMediaPredicateComparisonEqualTo];

   [MPMediaSongQuery addFilterPredicate:iPodMusicSongPredicate];

   [collections addObjectsFromArray:MPMediaSongQuery.collections];

   [MPMediaSongQuery removeFilterPredicate:iPodMusicSongPredicate];

}

...

于 2011-04-20T06:11:25.100 に答える