2009-11-17 62 views
6

我有我的應用程序顯示MPMediaPickerController。我想保存MediaItem並在啓動時重新開始播放。我認爲這是可行的使用MPMediaQuery。看來,我應該使用MPMediaItemPropertyPersistentID,但我不確定如何查詢它。有什麼想法嗎?iPhone sdk,保存MPMediaItemCollection?

回答

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]; 
} 
+0

helll是啊你殺了它! – 2012-10-23 00:09:39

+0

是否需要將單個MPMediaItem包裝到MPMediaItemCollection中? – surfrider 2016-02-17 20:03:58

6

這應該工作:

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

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