0

I am using the MPMusicPlayerController to play songs from Apple Music. I am periodically (every several seconds) changing the song queue using

setQueue(with: MPMusicPlayerStoreQueueDescriptor(storeIDs: ids))

I don't have any problems during music playback, the song queue is updated as requested, I can skip to next song and everything is fine. However, whenever I pause the player in the middle of the song, wait a bit till a new queue is set, and play again - the current song is lost and next song starts to play!

So imagine the following situation:

  1. I have songs A, B, C, D
  2. I set it as a song queue and call play()
  3. player.nowPlayingItem returns A
  4. I set the song queue to E, F, G, H while playing
  5. I call player.skipToNext() - song E starts playing, as expected
  6. I call player.pause() - song E pauses
  7. I call player.play() - song E continues to play. Everything is fine till now
  8. I call player.pause() again - song E pauses
  9. I set the song queue to I, J, K, L.
  10. I call player.play() - I expect the paused song E to continue playing. Instead, song I starts playing

I also did some log output for the above scenario:

func togglePlayPause() {
    if player.playbackState == .playing {
        player.pause()
    } else {
        NSLog("NP before \(player.nowPlayingItem)") // prints E at step 10
        player.play()
        NSLog("NP after \(player.nowPlayingItem)") // prints nil at step 10
    }
}

Strangely, pause/play from lock screen works fine, even if I change the queue in between.

Has anyone experienced similar problem, any hints/workarounds how to fix this?

frangulyan
  • 3,580
  • 4
  • 36
  • 64

1 Answers1

1

As a workaround, I used

player.currentPlaybackRate = 1

instead of player.play(). Seems like play() does a bit more than just playing from paused location.

frangulyan
  • 3,580
  • 4
  • 36
  • 64