ios - MPMusicPlayerControllerNowPlayingItemDidChange 是不可预测的
问题描述
基本上 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.
解决方案
推荐阅读
- assembly - x86 程序集加密到解密
- javascript - 防止复制过去并在 Blackboard 中单击鼠标右键
- c# - 即使我们关闭应用程序,WPF应用程序也会运行后台,任何人都可以建议我
- python - 在 pythonanywhere.com 中运行 WSGI 应用程序时出错
- python - 用美丽的汤在 HTML 标记之间获取文本
- jquery - 仅将类添加到视口中的元素
- android - 如何在 Android 应用程序的日历视图中添加事件?
- python - 如何在 python 上使用带有 selenium 的希伯来语?
- awk - 根据使用 awk 对先前记录的计数更新特定列中的字段
- sql - 如何计算分组查询中每行 COUNT(*) 总和的百分比?- SQL