Question

I have playlists songs in NSArray and show that songs in my UITableView like following pic.

enter image description here

In my case, when i select one song from UITableView , i want to play that song with MPMusicPlayerController's applicationMusicPlayer.

I mean when i select american idiot from UITableView , i want to play American idiot with MPMusicPlayerController's

And when i tap below Skip button , it's must be play next song like jesus of suburbia.

Here is my code that load songs to UITableView

self.player = [MPMusicPlayerController applicationMusicPlayer];

    MPMediaQuery *query = [MPMediaQuery playlistsQuery];
    MPMediaPredicate *predicate = [MPMediaPropertyPredicate predicateWithValue:@"GreenDay" forProperty:MPMediaPlaylistPropertyName comparisonType:MPMediaPredicateComparisonContains];

    [query addFilterPredicate:predicate];

    self.arrayOfSongs = [query items];

i think you understand what i mean. I want to do all music button work like iOS build in Music app and play song when i select song from UITableView.

I am trying anyting , but i didn't find solutions that can solve my problem.

Please help me to do like that.

Thanks you. :)

Was it helpful?

Solution

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];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top