3

On initial startup of the app, the first time any song is selected, the app never plays the actual song selected.

The app will start playing whatever song was last playing in the Music app for some reason. Even though I'm passing the selected song to it and everything is logging in the console fine.

But then everything works fine from then on, and the app plays the song selected.

I have no idea what is going on though, any ideas?

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"nowPlaying"]){
        // send to now playing
        NSUInteger selectedSection = [[self.tableView indexPathForSelectedRow] section];
        NSUInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];        
        NSArray *albumTracksArray = [self albumTracksForSegue:[[albumsArrayForTVC objectAtIndex:selectedSection] representativeItem]];
        MPMediaItem *rowItemSong = [[albumTracksArray objectAtIndex:selectedIndex] representativeItem];
        MPMusicPlayerController *musicPlayer = [MPMusicPlayerController systemMusicPlayer];
        [musicPlayer setQueueWithItemCollection:[MPMediaItemCollection collectionWithItems:albumTracksArray]];

    if ([musicPlayer nowPlayingItem] == rowItemSong) {
        // Nothing
        NSLog(@"These songs are equivalent: %@", [musicPlayer nowPlayingItem]);
        [musicPlayer setNowPlayingItem:rowItemSong];
        [musicPlayer play];
        NSLog(@"Row Item Song Same: %@", rowItemSong);
    } else {
        [musicPlayer setNowPlayingItem:rowItemSong];
        [musicPlayer play];
        NSLog(@"Row Item Song Different: %@", rowItemSong);
        }
    }
}
SRMR
  • 3,064
  • 6
  • 31
  • 59
  • 1
    Try skipToNextItem instead of setItem and play. – john elemans Jul 07 '16 at 23:11
  • @johnelemans are you saying something like `[musicPlayer skipToNextItem]` instead of `[musicPlayer setNowPlayingItem:rowItemSong];` + `[musicPlayer play];`? – SRMR Jul 14 '16 at 00:07
  • yes, that's what I meant. alexcurylo's answer is interesting! – john elemans Jul 14 '16 at 15:38
  • @johnelemans awesome, thanks so much! – SRMR Jul 14 '16 at 17:15
  • @johnelemans Just wondering though, this way of doing it would always skip to the next song? Because in my original problem, the weird edge case was only happening on the first time the app runs, so I wouldn't want it to always skip to the next song because it wouldn't be playing the song that was selected. – SRMR Jul 15 '16 at 01:07

1 Answers1

2

There's a few funny edge cases that turn up when assigning queues to the music player, and they vary by System version. A trick that often helps is after you set the queue, call

[musicPlayer setCurrentPlaybackTime: 0];

Give that a shot and see if it works for you.

Alex Curylo
  • 4,744
  • 1
  • 27
  • 37
  • This is really good, and it worked the first time, so I'll keep testing it. Randomly wondering how you've come across the edge cases like this before? There's little documentation, and not many other people using it that put there code out there, so it would have taken me forever to potentially come to this without you helping me out and saving me? – SRMR Jul 14 '16 at 00:09
  • 1
    It so happened that I was writing a playlist-using app back when OS 3 came out, so I vaguely remembered the kind of thing that helped back with the first few revisions of the API. But mostly in general it's that "eliminate all possible internal state of the API" is a sound way to approach the problem of isolating/eliminating unexpected behaviour. – Alex Curylo Jul 14 '16 at 14:46
  • Wow, thats great, and a great way of approaching it too! Thanks a bunch – SRMR Jul 14 '16 at 17:16
  • Been testing this more and a similar thing happened: I ran the app fresh, selected a song, but the app shows the last song that was playing in the Music app earlier today. The only small different was that the last song that was playing in the Music app earlier today didn't start playing and wouldn't allow me to skip back/forward to other songs. Any more ideas? – SRMR Jul 15 '16 at 01:05
  • 1
    Hmmmmm. That's a new one to me. But doing a quick search, I see a number of [questions like this](http://stackoverflow.com/questions/37281520/ios-9-3-2-breaks-mpmusicplayercontroller?rq=1) indicating that wow, MPMusicPlayerController is messed up in all sorts of ways in iOS 9. Try running against iOS 10 and filing Radars so the engineers can get assigned time to fix it! – Alex Curylo Jul 15 '16 at 13:20
  • Crazy. Thanks for the link, thats much appreciated, at least to know I'm not the only one going crazy with this – SRMR Jul 15 '16 at 13:24