2009-11-17 16 views
6

Ho la mia applicazione che visualizza MPMediaPickerController. Vorrei salvare lo MediaItem e avviarlo di nuovo all'avvio. Penso che sia fattibile usando lo MPMediaQuery. A quanto pare, dovrei usare lo MPMediaItemPropertyPersistentID ma non sono sicuro di come farlo. qualche idea?sdk iPhone, salvare MPMediaItemCollection?

risposta

11

uomo, non avete bisogno di salvare mediaCollection. raccolta multimediale è solo una matrice di oggetti MPMediaItem. Quindi faresti meglio a salvare gli ID persistenti di questi elementi. è abbastanza semplice

//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 yeah l'hai ucciso !!! –

+0

è necessario per avvolgere il singolo MPMediaItem in MPMediaItemCollection? – surfrider

6

Questo dovrebbe funzionare:

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

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