基本上 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.