It doesn't skip to the next song because the playback queue only has one item. At launch, set the playback queue to the entire songs query. Also create an array of MPMediaItem
s using the items
property of MPMediaQuery
:
MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
self.songs = songsQuery.items;
[self.musicPlayer setQueueWithQuery:songsQuery];
In tableView:didSelectRowAtIndexPath:
, have the music player play the selected item using MPMusicPlayerController
's setNowPlayingItem:
method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MPMediaItem *song = self.songs[indexPath.row];
[self.musicPlayer setNowPlayingItem:song];
[self.musicPlayer play];
}
Here is a sample project that demonstrates this: https://dl.dropboxusercontent.com/u/1990969/SongsPlayerTableView.zip