Getting a specific song from a specific playlist
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?
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;
}
}
精彩评论