获取特定歌曲从一个特定的播放列表(Getting a specific song from a sp

2019-11-02 17:05发布

比方说,我想通过名字顺序从一个特定的所有歌曲,说:“我的播放列表”,播放列表,并且在播放列表播放歌曲100。

是否有可能呢?

Answer 1:

我没有测试过这一点,但下面的代码是一个开始检索播放列表和播放其100项。 然而,这并不排序蒂特播放列表。 对于这一点,你可以遍历播放列表项,检索所有他们的名字,并把名字与一个字典MPMediaItem对象作为键(不知道这是否正常工作)。 然后,您可以通过排序通过调用歌名-keysSortedByValueUsingSelector:词典中,它返回的媒体项目的数组。 从这个数组就拿100元和饲料它的音乐播放器。

NSString *playlistToPlay = @"My playlist";
MPMediaQuery *playlistsQuery = [MPMediaQuery playlistsQuery];
NSArray *playlists = [playlistsQuery collections];
for (MPMediaPlaylist *playlist in playlists) {
    NSString *playlistName = [playlist valueForProperty:MPMediaPlaylistPropertyName];
    if ([playlistName isEqualToString:playlistToPlay]) {
        // This is the playlist we are looking for
        MPMusicPlayerController *player = [MPMusicPlayerController iPodMusicPlayer];
        [player stop];
        [player setQueueWithItemCollection:playlist];

        // Play the 100th song in the playlist
        MPMediaItem *songToPlay = [[playlist items] objectAtIndex:99];
        player.nowPlayingItem = songToPlay;
        [player play];

        // Exit the loop
        break;
    }
}


文章来源: Getting a specific song from a specific playlist