Ios4 如何在iPhone音乐播放器框架中使用MPMediaItemPropertyPersistentID播放音乐?

Ios4 如何在iPhone音乐播放器框架中使用MPMediaItemPropertyPersistentID播放音乐?,ios4,Ios4,我的代码成功地为整个音乐库的歌曲名称和ID分类。但是,它不会使用此方法实际播放歌曲,并且控制台显示以下内容: 消息playbackState超时 self.musicPlayer = [MPMusicPlayerController applicationMusicPlayer]; MPMediaQuery *everything = [[MPMediaQuery alloc] init]; NSArray *itemsFromGenericQuery = [everything items]

我的代码成功地为整个音乐库的歌曲名称和ID分类。但是,它不会使用此方法实际播放歌曲,并且控制台显示以下内容:

消息playbackState超时

self.musicPlayer = [MPMusicPlayerController applicationMusicPlayer];

MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSArray *itemsFromGenericQuery = [everything items];
SongName = [[NSMutableArray alloc] init];
SongItem = [[NSMutableArray alloc] init];
NSString *songTitle;
NSString *songID;
//Collect names & ID for entire music library & put into arrays
for (MPMediaItem *song in itemsFromGenericQuery) {
songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
[SongName addObject:songTitle];
songID = [song valueForProperty: MPMediaItemPropertyPersistentID];
[SongItem addObject:songID];
}

NSLog (@"%@", [SongName objectAtIndex:1]);
NSLog (@"%@", [SongItem objectAtIndex:1]);
// Play the second song in the list
MPMediaItemCollection *collection = [MPMediaItemCollection collectionWithItems:[NSArray arrayWithObject:[SongItem objectAtIndex:1]]];
[self.musicPlayer setQueueWithItemCollection:collection];
[self.musicPlayer play];
消息nowPlayingItem超时

self.musicPlayer = [MPMusicPlayerController applicationMusicPlayer];

MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSArray *itemsFromGenericQuery = [everything items];
SongName = [[NSMutableArray alloc] init];
SongItem = [[NSMutableArray alloc] init];
NSString *songTitle;
NSString *songID;
//Collect names & ID for entire music library & put into arrays
for (MPMediaItem *song in itemsFromGenericQuery) {
songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
[SongName addObject:songTitle];
songID = [song valueForProperty: MPMediaItemPropertyPersistentID];
[SongItem addObject:songID];
}

NSLog (@"%@", [SongName objectAtIndex:1]);
NSLog (@"%@", [SongItem objectAtIndex:1]);
// Play the second song in the list
MPMediaItemCollection *collection = [MPMediaItemCollection collectionWithItems:[NSArray arrayWithObject:[SongItem objectAtIndex:1]]];
[self.musicPlayer setQueueWithItemCollection:collection];
[self.musicPlayer play];

我将再次回答我自己的问题。问题是collectionWithItems:需要一个MPMediaItems数组,而不是一个MPMediaItemPropertyTyperSistenId数组。以下是可能有相同问题的任何人的工作代码:

MPMediaQuery *everything = [[MPMediaQuery alloc] init];
NSArray *itemsFromGenericQuery = [everything items];
SongItem = [[NSMutableArray alloc] init];
for (MPMediaItem *song in itemsFromGenericQuery) {
   NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];
   //NSLog (@”%@”, songTitle);
   songID = [song valueForProperty: MPMediaItemPropertyPersistentID];
   //NSLog (@”%@”, songID);
   [SongItem addObject:songID];
}

//Choose the first indexed song
NSString *selectedTitle = [SongItem objectAtIndex:0];

//Use the MPMediaItemPropertyPersistentID to play the song
MPMediaPropertyPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:selectedTitle forProperty:MPMediaItemPropertyPersistentID];
MPMediaQuery *mySongQuery = [[MPMediaQuery alloc] init];
[mySongQuery addFilterPredicate: predicate];
[musicPlayer setQueueWithQuery:mySongQuery];
[musicPlayer play];

上面的代码只有一个小问题(尽管不会特别影响这个示例)。与MPMediaItemPropertyPersistentID关联的类型是NSNumber,而不是NSString。因此,selectedTitle的类型应为NSNumber*。以下是苹果公司的文档:()但我们如何在iPhone 5s中获取MPMediaItemPropertyTypersistentId的值。对于我设备中的一些音乐,它会返回一个负曲目id,因为该值溢出。我使用的NSNumber编号如下: