在我的应用程序中,我想将用户播放列表保存在 plist 文件中,并在应用程序启动时加载此播放列表。
这是我用来保存的代码:
if (self.backgroundMusicItems) {
NSMutableArray *songsID = [[NSMutableArray alloc] initWithCapacity:0];
MPMediaItem *item;
for (item in self.backgroundMusicItems.items) {
NSNumber *songId = [item valueForProperty:MPMediaItemPropertyPersistentID];
[songsID addObject:songId];
}
NSString *documentdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentdir stringByAppendingPathComponent:@"playlist.plist"];
[songsID writeToFile:filePath atomically:YES];
}
这是我用来加载的代码:
NSString *documentdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentdir stringByAppendingPathComponent:@"playlist.plist"];
NSArray *myArray = [[NSArray alloc] initWithContentsOfFile:filePath];
NSMutableSet *predicates = [[NSMutableSet alloc] initWithCapacity:0];
if (myArray.count > 0) {
NSNumber *songId;
for (songId in myArray) {
MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:songId forProperty:MPMediaItemPropertyPersistentID];
[predicates addObject:predicate];
}
MPMediaQuery *query = [[MPMediaQuery alloc] initWithFilterPredicates:predicates];
NSArray *mediaItems = [query items];
if (mediaItems.count > 0) [GameOptions sharedClass].backgroundMusicItems = [[MPMediaItemCollection alloc] initWithItems:mediaItems];
}
myArray 包含 2 个项目,但 [query items] 方法返回 0 个项目。我哪里错了??