I'm currently attempting to make a music player app for iOS on Xcode. I have been able to show song information on the lockscreen / control center and made my music player play the next song when the user presses the next button on the lockscreen / control center.
BUT Every other time I press the next button the song is changed and begins to play normally, but the nowPlayingInfo does not update on the lockscreen / control center. I have made a method that rechecks (after 1.5s of pressing the next button) to make sure the lockscreen / control center displays the current song and if not it retrys to set the song data on the nowPlayingInfo (It works but it makes you have to wait 1.5s to see the song info every other time when the info does not update). I have seen how the default Music app and other music players almost instantly switch to the next song when you press the next button.
Why is the nowPlayingInfo not updating sometimes? I have made many tests to make sure the code where the nowPlayingInfo is set is called when the next button is pressed, but the info is not updated on the lockscreen / control center every other time (specially if you spam the next button; which i put a 0.4 second delay to be able to play the next song).
Other times the info is updated but it takes over a second to show.
This is how I update the song info
EDIT
I have fixed the issue. Before: I updated the info center directly to MPNowPlayingInfoCenter.nowPlayingInfo every time I pressed next button, previous button, pause, and play. This would, for some reason, cause the info center to slow or even fail to update if you were calling to this in quick successions.
Now: I have my own variable of type [String: Any]? called myNowPlayingInfo (which is the type of the MPNowPlayingInfoCenter.nowPlayingInfo). I also created a 'needsToUpdateInfoCenter' boolean. Every time the user makes a song-info related change, the 'myNowPlayingInfo' variable I created is updated and I set 'needsToUpdateInfoCenter' to true. I have a Timer that checks to see if 'needsToUpdateInfoCenter' is true every 0.5 seconds and if it is true I just set MPNowPlayingInfoCenter.nowPlayingInfo = myNowPlayingInfo . :)