Getting a specific song from a specific playlist

2019-08-25 17:48发布

Let's say i want to order by name all songs from a specific, say "My playlist", Playlist, and play song 100 in that playlist.

Is it possible at all?

1条回答
对你真心纯属浪费
2楼-- · 2019-08-25 18:45

I haven't tested this, but the code below is a start to retrieve a playlist and play its 100th item. However, it does not sort the playlist by tite. For that, you could iterate over the playlist items, retrieve all their names, and put the names into a dictionary with the MPMediaItem objects as keys (don't know if this works). You can then sort by the song titles by calling -keysSortedByValueUsingSelector: on the dictionary, which returns an array of media items. Take the 100th element from this array and feed it to the music player.

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;
    }
}
查看更多
登录 后发表回答