Basically the MPMusicPlayerControllerNowPlayingItemDidChange is very unpredictable. Sometimes it gets called once, sometimes twice, sometimes it gets randomly called in the middle of a song while it's playing. I need to figure out a way to ignore the fake notification calls and only handle the times that a music item was actually changed from one item to another.
I've tried seeing if the currentPlaybackTime == 0 and only then handling the notification but sometimes the songs updated and their playback time was not 0. I also tried creating a buffer that only allows the new item to update a certain time after the old item was updated but that is really unreliable because in rare cases, the notification gets sent in the middle of a song when I prepend the music player queue with new songs.
@objc static func appleMusicItemDidUpdate() {
let spController = AppDelegate.songPlayerController
let item = AppDelegate.appleMusicPlayer.nowPlayingItem
print("got new item:", item?.title ?? "no title")
if let item = item, let title = item.title, title != "Loading...", AppDelegate.appleMusicPlayer.currentPlaybackTime == 0 {
if item.isCloudItem && item.playbackStoreID != "0" && item.playbackStoreID != "" {
print("test1")
if /*other specific check (not important)*/ true {
print("handling notification")
}
} else {
print("test5")
spController.lastUpdate = Date().timeIntervalSince1970
print("handling notification")
}
} else {
print("item did not pass check:")
if item == nil {
print("item was nil")
}
if item?.title == nil {
print("title was nil")
}
if item?.title == "Loading..." {
print("item was loading")
}
if AppDelegate.appleMusicPlayer.currentPlaybackTime >= 0.0001 {
print("current playback time was", AppDelegate.appleMusicPlayer.currentPlaybackTime)
}
if !spController.passesBufferCheck() {
print("did not pass buffer check")
}
print("")
}
The notification gets called randomly and sometimes gets called multiple times when a song is updated and I can't find a reliable way to check for an actual update.