Assuming that your NSArray was populated with MPMediaItems from a MPMediaItemCollection, this is actually fairly simple once you know what you need to configure. First, create a iVar, preferably NSUInteger to store the index of the currently playing track. This will be necessary to go from one track to another in order.
Second, it's hard to tell from your post wether the track titles are being read from the media items in the collections or are if they're just statically placed on the table, but I've included an example of how to read a track title from the media items in the array and set that value as the cells text using valueForProperty
.
And finally, didSelectRowAtIndexPath
is configured below to demonstrate of the set the unsigned integer that you've already created to the value of the selected cell. Both didSelectRowAtIndexPath
and the two IBActions I've created both modify the value of this index and then call the void I've created "stopCurrentTrackAndPlaySelection" which stops the currently playing track, typecasts MPMediaItem to the object at that index, and then starts playing again.
If you need more clarification just ask :)
Side note: I recommend that you store a copy of the media pickers selection (MPMediaItemCollection) in a NSMutableArray instead of NSArray, or MPMediaItemCollection directly. This will allow you to add or remove tracks on the fly without having to stop playback.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myArrayOfTracks count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[[cell textLabel] setText:[(MPMediaItem *)[myArrayOfTracks objectAtIndex:indexPath.row] valueForProperty:MPMediaItemPropertyTitle]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
indexOfCurrentlyPlayingTrack = indexPath.row;
[self stopCurrentTrackAndPlaySelection];
}
- (IBAction)nextTrack:(UIButton *)sender
{
indexOfCurrentlyPlayingTrack ++;
[self stopCurrentTrackAndPlaySelection];
}
- (IBAction)previousTrack:(UIButton *)sender
{
indexOfCurrentlyPlayingTrack --;
[self stopCurrentTrackAndPlaySelection];
}
- (void)stopCurrentTrackAndPlaySelection
{
[myMPMusicPlayerController stop];
[myMPMusicPlayerController setNowPlayingItem:(MPMediaItem *)[myArrayOfTracks objectAtIndex:indexOfCurrentlyPlayingTrack]];
[myMPMusicPlayerController play];
}