基本上,MPMusicPlayerControllerNowPlayingItemDidChange非常难以预测。有时它会被调用一次,有时会被调用两次,有时会在播放时在歌曲中间随机调用。我需要找出一种方法来忽略虚假的通知呼叫,而只处理音乐项目实际上从一个项目更改为另一个项目的时间。
我尝试查看currentPlaybackTime == 0,然后才处理通知,但有时歌曲已更新且其播放时间不为0。我还尝试创建仅允许新项更新一定时间的缓冲区在更新旧项目之后,但这确实不可靠,因为在极少数情况下,当我在音乐播放器队列中添加新歌曲时,通知会在歌曲中间发送。
@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.