0

I am trying to make a music player, this is where I've put the NSLog to show the current index:

- (void) handle_NowPlayingItemChanged: (id) notification
{
    MPMediaItem *currentItem = [musicPlayer nowPlayingItem];
    NSUInteger currentIndex = [musicPlayer indexOfNowPlayingItem];
    UIImage *artworkImage = [UIImage imageNamed:@"NoSongImage.png"];
    MPMediaItemArtwork *artwork = [currentItem valueForProperty:MPMediaItemPropertyArtwork];

if (artwork) {
    artworkImage = [artwork imageWithSize: CGSizeMake (200, 200)];
}

[self.artwork setImage:artworkImage];

NSString *titleString = [currentItem valueForProperty:MPMediaItemPropertyTitle];
if (titleString) {
    self.songName.text = [NSString stringWithFormat:@"%@",titleString];
} else {
    self.songName.text = @"Unknown title";
}

    NSLog(@"%li", currentIndex);
}

this is my didSelectRowAtIndexPath for my UITableView:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (songsList == YES){
    NSUInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
    MPMediaItem *selectedItem = [[songs objectAtIndex:selectedIndex] representativeItem];
    [musicPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:[songsQuery items]]];
    [musicPlayer setNowPlayingItem:selectedItem];
    [musicPlayer play];
    songsList = NO;
}
else if (albumsList == YES && albumsSongsList == NO){
    albumsSongsList = YES;
    NSUInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
    MPMediaItem *selectedItem = [[albums objectAtIndex:selectedIndex] representativeItem];
    albumTitle = [selectedItem valueForProperty:MPMediaItemPropertyAlbumTitle];
    MPMediaItemArtwork *albumArtwork = [selectedItem valueForProperty:MPMediaItemPropertyArtwork];
    albumSelected = [albumArtwork imageWithSize: CGSizeMake (44, 44)];
    [self.tableView reloadData];
    [self.tableView setContentOffset:CGPointZero animated:NO];
}
else if (albumsSongsList == YES){
    albumsSongsList = NO;
    MPMediaQuery *albumQuery = [MPMediaQuery albumsQuery];
    MPMediaPropertyPredicate *albumPredicate = [MPMediaPropertyPredicate predicateWithValue: albumTitle forProperty: MPMediaItemPropertyAlbumTitle];
    [albumQuery addFilterPredicate:albumPredicate];
    NSArray *albumTracks = [albumQuery items];
    NSUInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
    MPMediaItem *selectedItem = [[albumTracks objectAtIndex:selectedIndex] representativeItem];
    [musicPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:[albumQuery items]]];
    [musicPlayer setNowPlayingItem:selectedItem];
    [musicPlayer play];
    }
}

The BOOLS determine to the tableView willDisplayCell which table to load. The above all display the UITableView correctly with the correct content. The problem is, when I open Albums, and select a song, it seems to play the first song of the album (at the top of the tableView) instead. If I then again go back to albums and select a different song, the correct song is selected. Then again, if I go back once more and select a song, the first song is played again and repeats so on...

In my NSLog, the reason its playing the first song due to a currentIndex of 9223372036854775807

Why is it returning NSNotFound? The selectedIndex (e.g 4) is in the array of albumTracks. This happens 1/3 times for the songs also. Your help is very much appreciated.

Manesh
  • 528
  • 6
  • 20

2 Answers2

1

What is the value of NSNotFound on a 64 bit system? What is 2^63 - 1?

gnasher729
  • 51,477
  • 5
  • 75
  • 98
0

Try to pause or stop your musicplayer before setting nowPlayingItem. Sometimes the Shuffle mode can change the index too, try to turn it off.

bennmen
  • 1
  • 3