我的应用程序显示MPMediaPickerController
。我想保存MediaItem
并在启动时重新开始播放。我认为使用MPMediaQuery
是可行的。看来,我应该使用MPMediaItemPropertyPersistentID
,但我不知道如何查询它。有什么想法吗?
答案 0 :(得分:11)
伙计,你不需要保存mediaCollection。媒体集合它只是MPMediaItem对象的数组。所以你最好保存这些项目的persistentIds。这很容易
//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];
}
答案 1 :(得分:6)
这应该有效:
MPMediaQuery *query = [MPMediaQuery songsQuery];
MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:myPersistentID forProperty:MPMediaItemPropertyPersistentID];
[query addFilterPredicate:predicate];
NSArray *songs = [query items];