i'm developing a music player app.I have a tableView that contains the songs names of the ipod library.My problem is that when i select a cell it plays a specific song but when that song is completed it doesn't skip to the next item automatically. This is my code when i select a cell:
MPMediaQuery *mq = [MPMediaQuery songsQuery];
MPMediaPropertyPredicate *songNamePredicate = [MPMediaPropertyPredicate
predicateWithValue:[songsTitle objectAtIndex:indexPath.row]
forProperty:MPMediaItemPropertyTitle];
[mq addFilterPredicate:songNamePredicate];
[mp setQueueWithQuery:mq];
[mp play];
i really need your help.Thanks.
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