I agree with the posters here – it is very surprising that we are not able to get a notification for when the queue has ended.
I imagine the MPMusicPlayer framework can emit a notification like "MPMusicPlayerControllerQueueEnded" – it is clearly doing its own cleanup operations when a queue actually ends, so it should be straightforward to add this in.
I have hacked around this with a non-ideal solution, one that I'm afraid this might not cover all edge cases.
I run this on receiving a MPMusicPlayerControllerPlaybackStateDidChange notification:
if player.playbackState == .paused,
let currentTrack = currentTrack,
currentTrack == queue.last,
player.currentPlaybackTime >= currentTrack.unwrappedDuration - 1 {
EndOfQueueManager.handler()
}
And I run this on receiving a MPMusicPlayerControllerNowPlayingItemDidChange notification:
if UIApplication.shared.applicationState != .active,
queuePosition == queue.count - 1 {
EndOfQueueManager.handler()
}
currentTrack, queuePosition and queue are objects that I (reluctantly) maintain, but that's pretty much to work around the limitations of the API.