2

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

我正在尝试从特定年份(即 2002 年)收集用户 iPhone 音乐库中的所有歌曲。

然后我希望通过 MPMusicPlayerController 播放歌曲。

似乎您无法设置 MPMediaPropertyPredicate 来按发布日期进行过滤,我认为这排除了这一点。我真正不想做的是必须获得所有曲目发布日期的完整数组,然后迭代所有 NSDates,因为我(可能错误地)预计这对于大型库来说可能会很慢。

完成这项任务的最佳方法是什么?

提前致谢。

4

1 回答 1

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];
于 2011-05-13T22:18:01.117 回答