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

发布于 2024-10-01 08:48:12 字数 305 浏览 6 评论 0原文

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

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

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

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

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

提前致谢。

Hi Stack Overflowers, I'm hoping you might be able to help me.

I'm trying to get a collection of all songs in a users iPhone music library from a specific year i.e 2002.

I'm then looking to play the songs through a MPMusicPlayerController.

It seems that you can't set up a MPMediaPropertyPredicate to filter by release date which I think rules that out. What I don't really want to do is have to get a full array of all track release dates and then iterate all the NSDates as I (perhaps wrongly) expect this could be quite slow for large libraries.

What is the best way of achieving this task?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方。的韩爷 2024-10-08 08:48:12

这就是您正在寻找的东西。

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];

here is what you are looking for.

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文