Hello, this is building off of another post in which several other posters and I had already attempted solving the issue in hacky ways.
I am using MPMusicPlayerController.applicationQueuePlayer.
My end goal here is to dynamically add items to the queue when it has ended based on my application's business logic. There is no way for me to know what these items will be when I am initially setting the queue.
I have an updated implementation that seems to cover most edge cases, except for a glaringly obvious one – if there is just one item in the queue, and the user skips the track via MPRemoteCommandCenter (eg. lock screen), then it does not work.
Currently, when I receive a MPMusicPlayerControllerPlaybackStateDidChange notification, I run this block:
if player.playbackState == .paused,
player.currentPlaybackTime == 0,
player.indexOfNowPlayingItem == 0 {
EndOfQueueManager.handle()
}
In the absence of a mechanism to detect the end of the queue from the framework, I would love to add the ability to add a target to MPRemoteCommand, like you can do for AVPlayer. I have tried to do exactly that, but it does not work:
MPRemoteCommandCenter.shared().nextTrackCommand.addTarget { (event) -> MPRemoteCommandHandlerStatus in
if queue.count == 1 {
EndOfQueueManager.handle()
}
return .success
}
I already have a functioning AVPlayer implementation that achieves my goal without any compromises or edge cases. I would be very disappointed if there is no way to do this with MPMusicPlayerController – being notified about the queue ending feels like a fairly rudimentary API hook.