Finally I found the solution:
SAVE:
- (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection {
[self savePlaylist:mediaItemCollection];
[self dismissModalViewControllerAnimated: YES];
}
- (void)savePlaylist:(MPMediaItemCollection *) mediaItemCollection {
NSArray* items = [mediaItemCollection items];
NSMutableArray* listToSave = [[NSMutableArray alloc] initWithCapacity:0];
for (MPMediaItem *song in items) {
NSNumber *persistentId = [song valueForProperty:MPMediaItemPropertyPersistentID];
[listToSave addObject:persistentId];
}
NSData *data = [NSKeyedArchiver archivedDataWithRootObject: listToSave];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"songsList"];
[[NSUserDefaults standardUserDefaults] synchronize];
[listToSave release];
}
LOAD
- (void)setupMusic {
myPlayer = [MPMusicPlayerController applicationMusicPlayer];
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"songsList"] != nil) {
NSMutableArray *theList = [[NSMutableArray alloc] initWithCapacity:0];
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"songsList"];
NSArray *decodedData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
[theList addObjectsFromArray:decodedData];
NSMutableArray *allTheSongs = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < [theList count]; i++) {
MPMediaQuery *songQuery = [MPMediaQuery songsQuery];
[songQuery addFilterPredicate:[MPMediaPropertyPredicate predicateWithValue:[theList objectAtIndex:i] forProperty:MPMediaItemPropertyPersistentID]];
NSArray *songs = [songQuery items];
[allTheSongs addObjectsFromArray: songs];
}
MPMediaItemCollection *currentQueue = [[MPMediaItemCollection alloc] initWithItems:allTheSongs];
[myPlayer setQueueWithItemCollection:currentQueue];
[theList release];
[currentQueue release];
[allTheSongs release];
} else {
[myPlayer setQueueWithQuery: [MPMediaQuery songsQuery]];
[myPlayer setShuffleMode: MPMusicShuffleModeSongs]; //optional
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…