6

我的应用程序显示MPMediaPickerController. 我想保存MediaItem并在启动时重新开始播放。我认为使用MPMediaQuery. 看来,我应该使用,MPMediaItemPropertyPersistentID但我不知道如何查询它。有什么想法吗?

4

2 回答 2

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];
}
于 2010-11-19T13:49:01.637 回答
6

这应该有效:

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

[query addFilterPredicate:predicate];
NSArray *songs = [query items];
于 2009-11-19T16:43:29.933 回答