使用函数“addFilterPredicate”一次查询两首或更多歌曲。通过 MPMediaItemPropertyPersistentID

发布于 2024-10-30 17:55:47 字数 813 浏览 4 评论 0原文

我的应用程序启动时会自动加载音乐播放列表。为此,我将歌曲 ID MPMediaItemPropertyPersistentID 存储到数据库中,并在应用下次启动时加载歌曲。主要代码如下:

MPMediaQuery *MPMediaSongQuery = [MPMediaQuery songsQuery];

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

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

代码一首一首加载歌曲。我的问题是:在使用函数addFilterPredicate:时,有没有办法通过MPMediaItemPropertyPercientID一次查询两首或更多歌曲?谢谢。

My app loads a music playlist automatically when it starts up. In order to do this, I store the song IDs MPMediaItemPropertyPersistentID to a database, and load the songs when the app is starting up next time. The main code is the following:

MPMediaQuery *MPMediaSongQuery = [MPMediaQuery songsQuery];

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

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

The code loads song one by one. My question is: is there any way to query two or more songs by MPMediaItemPropertyPersistentID at one time when using the function addFilterPredicate:? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习ぎ惯性依靠 2024-11-06 17:55:47

如果您使用多个 addFilterPredicate,它们将通过逻辑 AND 组合在一起。所以你只能减少第一个查询的结果,而不能扩展它。因此,您不允许对同一属性使用多个 addFilterPredicates。事实上,结果是未定义的,很可能最终会出现一个空集合。您正在寻找的是相同属性与逻辑或的组合。您可以像下面的伪代码所示来实现这一点:

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];

}

...

If you use more than one addFilterPredicate they are combined with a logical AND. So you canm only reduce the results of the first query, but not extend it. As a consequence you are not allowed to use several addFilterPredicates for the same property. In fact the result is undefined and most likely will end up in an empty collection. What you are looking for is a combination of the same property with a logical OR. You can achieve this like depicted in the following pseudo-code:

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];

}

...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文