gpt4 book ai didi

objective-c - 播放iPod Library中的歌曲

转载 作者:行者123 更新时间:2023-12-01 17:35:04 28 4
gpt4 key购买 nike

我正在尝试播放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];

它什么都不玩。我有很多歌曲标题,如果存在,我想播放它们。

我怎样才能做到这一点?

谢谢。

最佳答案

这是解决方案。播放艺术家的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];

关于objective-c - 播放iPod Library中的歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6028467/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com