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:
- I have songs
A, B, C, D
- I set it as a song queue and call
play()
player.nowPlayingItem
returnsA
- I set the song queue to
E, F, G, H
while playing - I call
player.skipToNext()
- songE
starts playing, as expected - I call
player.pause()
- songE
pauses - I call
player.play()
- songE
continues to play. Everything is fine till now - I call
player.pause()
again - songE
pauses - I set the song queue to
I, J, K, L
. - I call
player.play()
- I expect the paused songE
to continue playing. Instead, songI
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?