此问题上有一个答案:iOS - get programatically queue of items currently playing描述了如何执行此操作,但是我正在尝试该方法,但它不起作用。这是我的代码:
let numberOfItems = player.value(forKey: "numberOfItems") as! Int
for i in 0..<numberOfItems {
if let mediaItem = MPMusicPlayerController.systemMusicPlayer.perform(Selector("nowPlayingItemAtIndex:"), with: i)?.takeUnretainedValue() as? MPMediaItem {
let songObject = mediaItem.value(forKey: "propertyValues") as! NSObject
print(songObject.value(forKey: "title"))
print(mediaItem.title)
print(mediaItem.value(forKey: "title"))
print(mediaItem.value(forProperty: MPMediaItemPropertyTitle))
}
}
所有4条打印语句均为零。值得注意的是,它确实可以将项目强制转换为MPMediaItem,而songObject变量实际上将输出为MPModelSong。运行时标头指示MPModelSong对象“ title”(在此处:https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/MediaPlayer.framework/MPModelSong.h)上有一个属性
无论如何,所有内容均显示为零。知道我应该从这里往哪个方向走吗?