如何从特定年份获取iPhone音乐库中的所有歌曲?

时间:2010-11-10 10:37:36

标签: iphone audio ios mpmusicplayercontroller

嗨Stack Overflowers,我希望你能帮助我。

我试图从特定年份(即2002年)获取用户iPhone音乐库中所有歌曲的集合。

然后我想通过MPMusicPlayerController播放歌曲。

您似乎无法设置MPMediaPropertyPredicate来按发布日期进行过滤,我认为该规则已经过去了。我真的不想做的是获得所有跟踪发布日期的完整数组,然后迭代所有NSDates,因为我(可能错误地)期望这对于大型库来说可能非常慢。

实现这项任务的最佳方法是什么?

提前致谢。

1 个答案:

答案 0 :(得分:5)

这就是你要找的东西。

allMedia = [MPMediaQuery songsQuery];
//MPMediaPropertyPredicate *mpp1 = [MPMediaPropertyPredicate predicateWithValue:@"2"   forProperty:MPMediaItemPropertyRating comparisonType:MPMediaPredicateComparisonEqualTo];
//MPMediaPropertyPredicate *mpp2 = [MPMediaPropertyPredicate predicateWithValue:@"Pop" forProperty:MPMediaItemPropertyGenre comparisonType:MPMediaPredicateComparisonContains];
//[allMedia addFilterPredicate:mpp1];
//[allMedia addFilterPredicate:mpp2];

//[myPlayer setQueueWithQuery:allMedia];


NSArray *itemsFromGenericQuery = [allMedia items];

NSMutableArray *mArray = [[NSMutableArray alloc] init];
int i = 0;
int j=0;

NSLog(@"itemCount: %d",[itemsFromGenericQuery count]);

float playsQuery = sliderPlays.value;
if(playsQuery == 20){playsQuery = 10000;}

NSLog(@"sliderRating.value %f  sliderPlays.value %.1f", [sliderRating value],  playsQuery);

while(i++ < 1000){
    int trackNumber = arc4random() % [itemsFromGenericQuery count];
    MPMediaItem *song = [itemsFromGenericQuery objectAtIndex:trackNumber];

    NSString *artistName = [song valueForProperty: MPMediaItemPropertyArtist];
    NSString *title = [song valueForProperty: MPMediaItemPropertyTitle];
    NSString *rating = [song valueForKey:MPMediaItemPropertyRating];
    double length = [[song valueForProperty:MPMediaItemPropertyPlaybackDuration] doubleValue];
    NSNumber *year = [song valueForProperty:MPMediaItemPropertyYear];



    if ([year intValue] >= [def intValue] <= {
        if(j++ > 50){break;}
        NSLog (@"tracknumber: %d j: %d  artistName: %@ title: %@ length: %@ year: %@ playcount: %d",trackNumber, j, artistName, title, length, rating, [playCount intValue]);            
        [mArray addObject:song];            
    }

    if(i++ > 1000)break;
}


MPMediaItemCollection *itemCol = [[MPMediaItemCollection alloc] initWithItems:mArray];


[myPlayer setQueueWithItemCollection:itemCol];


[myPlayer setShuffleMode: MPMusicShuffleModeSongs];
[myPlayer setRepeatMode: MPMusicRepeatModeNone];