我有一个应用程序显示一个MPMediaPickerController
。我想在启动时保存MediaItem
并重新开始播放。我认为可以使用MPMediaQuery
来实现这一点。看起来,我应该使用MPMediaItemPropertyPersistentID
,但我不确定如何查询它。有什么想法吗?
我有一个应用程序显示一个MPMediaPickerController
。我想在启动时保存MediaItem
并重新开始播放。我认为可以使用MPMediaQuery
来实现这一点。看起来,我应该使用MPMediaItemPropertyPersistentID
,但我不确定如何查询它。有什么想法吗?
伙计,你不需要保存mediaCollection。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];
}
这应该可以工作:
MPMediaQuery *query = [MPMediaQuery songsQuery];
MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:myPersistentID forProperty:MPMediaItemPropertyPersistentID];
[query addFilterPredicate:predicate];
NSArray *songs = [query items];