iPhone sdk,保存MPMediaItemCollection吗?

发布于 2024-08-11 13:06:11 字数 201 浏览 9 评论 0原文

我的应用程序显示 MPMediaPickerController。我想保存 MediaItem 并在启动时再次播放。我认为使用 MPMediaQuery 可以做到这一点。看来,我应该使用 MPMediaItemPropertyPercientID 但我不知道如何查询它。有什么想法吗?

I have my application displaying a MPMediaPickerController. I would like to save the MediaItem and start it playing again on startup. I think this is doable using the MPMediaQuery. It seems, I should be using the MPMediaItemPropertyPersistentID but I am not sure how to query for it. Any thoughts?

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

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

发布评论

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

评论(2

旧瑾黎汐 2024-08-18 13:06:11

伙计,你不需要保存 mediaCollection。媒体集合它只是 MPMediaItem 对象的数组。所以你最好保存这个项目的permanentIds。这很容易

//it's how to know persistentId of the song after you got mediaItemCollection from your mediaPickerViewController
//then you can sav it in userDefaults.
- (NSNumber *)getPersistentId :(MPMediaItemCollection *)collection atIndex:(int)index {
 MPMediaItem *mediaItem = [collection.items objectAtIndex:index];
 NSNumber *anId = [mediaItem valueForProperty:MPMediaItemPropertyPersistentID];
 return anId;
}

//when your application will be launched next time you can get required song:
- (void)obtainSongWitId:(NSNumber *)persistentId {
 MPMediaQuery *query = [MPMediaQuery songsQuery];
 MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:persistentId forProperty:MPMediaItemPropertyPersistentID];
 [query addFilterPredicate:predicate];
 NSArray *mediaItems = [query items];
 //this array will consist of song with given persistentId. add it to collection and play it
 MPMediaItemCollection *col = [[MPMediaItemCollection alloc] initWithItems:mediaItems];
 ///....
 [col release];
}

Man, you dont need to save mediaCollection. media collection it is just array of MPMediaItem objects. So you'd better save persistentIds of this items. it's quite easy

//it's how to know persistentId of the song after you got mediaItemCollection from your mediaPickerViewController
//then you can sav it in userDefaults.
- (NSNumber *)getPersistentId :(MPMediaItemCollection *)collection atIndex:(int)index {
 MPMediaItem *mediaItem = [collection.items objectAtIndex:index];
 NSNumber *anId = [mediaItem valueForProperty:MPMediaItemPropertyPersistentID];
 return anId;
}

//when your application will be launched next time you can get required song:
- (void)obtainSongWitId:(NSNumber *)persistentId {
 MPMediaQuery *query = [MPMediaQuery songsQuery];
 MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:persistentId forProperty:MPMediaItemPropertyPersistentID];
 [query addFilterPredicate:predicate];
 NSArray *mediaItems = [query items];
 //this array will consist of song with given persistentId. add it to collection and play it
 MPMediaItemCollection *col = [[MPMediaItemCollection alloc] initWithItems:mediaItems];
 ///....
 [col release];
}
闻呓 2024-08-18 13:06:11

这应该有效:

MPMediaQuery *query = [MPMediaQuery songsQuery];
MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:myPersistentID forProperty:MPMediaItemPropertyPersistentID];

[query addFilterPredicate:predicate];
NSArray *songs = [query items];

This should work:

MPMediaQuery *query = [MPMediaQuery songsQuery];
MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:myPersistentID forProperty:MPMediaItemPropertyPersistentID];

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