从iPod库播放歌曲

时间:2011-05-17 08:55:39

标签: objective-c ios mpmusicplayercontroller

我正在尝试从iPod库中播放几首歌曲。我可以使用以下代码播放一首歌:

MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
MPMediaPropertyPredicate *artist = [MPMediaPropertyPredicate predicateWithValue:@"Poker Face" forProperty:MPMediaItemPropertyTitle];

MPMediaQuery *myArtistQuery = [[MPMediaQuery alloc] init];
[myArtistQuery addFilterPredicate:artist];

[musicPlayer setQueueWithQuery:myArtistQuery];
[musicPlayer play];

当我添加到之前的代码

时,它会播放一首歌“Poker Face”
MPMediaPropertyPredicate *artist1 = [MPMediaPropertyPredicate predicateWithValue:@"Born This Way" forProperty:MPMediaItemPropertyTitle]

[myArtistQuery addFilterPredicate:artist1]; 

一无所获。我有一系列歌曲标题,如果它们存在,我想播放它们。

我该怎么做?

谢谢。

1 个答案:

答案 0 :(得分:7)

这是解决方案。从艺术家的iPod库中播放两首歌曲

MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer]; MPMediaQuery *everything = [[MPMediaQuery alloc] init]; MPMediaPropertyPredicate *artist = [MPMediaPropertyPredicate predicateWithValue:@"AC/DC" forProperty:MPMediaItemPropertyArtist]; [everything addFilterPredicate:artist]; NSArray *itemsFromGenericQuery = [everything items]; NSMutableArray *array = [[NSMutableArray alloc] init]; for (MPMediaItem *song in itemsFromGenericQuery) { NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle]; if ([songTitle isEqualToString:@"Highway to Hell"]) { NSLog (@"%@", songTitle); [array addObject:song]; } if ([songTitle isEqualToString:@"Thunderstruck"]) { NSLog (@"%@", songTitle); [array addObject:song]; } } MPMediaItemCollection *collection = [[MPMediaItemCollection alloc] initWithItems:array]; [musicPlayer setQueueWithItemCollection:collection]; // NSLog(@"%@", [[MPMediaQuery songsQuery].items ); [musicPlayer play];