Getting a specific song from a specific playlist

三世轮回 提交于 2020-01-17 13:20: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:


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


来源:https://stackoverflow.com/questions/2020979/getting-a-specific-song-from-a-specific-playlist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!