2010-01-07 97 views

回答

3

我還沒有測試過,但下面的代碼是一個開始檢索播放列表並播放其第100個項目。但是,它不會按tite排序播放列表。爲此,您可以遍歷播放列表項目,檢索所有的名稱,並將名稱放入以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; 
    } 
}