使用PlayerItem.timedMetadata(粘贴如下)已经有一段时间了,并且效果很好。但是,苹果似乎已将此方法标记为“ iOS 13中已弃用”,并且可能(或将被删除)。
Xcode确实告诉我,我必须使用从未尝试过的另一种方法“ AVPlayerItemMetadataOutput”。因此,在互联网(google)上,除了苹果文档(https://developer.apple.com/documentation/avfoundation/avplayeritemmetadataoutput)之外,我什么都没发现。
override open func observeValue(forKeyPath: String?, of: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard forKeyPath == "timedMetadata" else { return }
guard let meta = PlayerItem.timedMetadata else { return }
for metadata in meta {
if let songName = metadata.value(forKey: "value") as? String {
Variables.MediaInfo = (songName)
self.MediaBox.text = ("Now Playing \n \(songName)")
setupNowPlaying()
}
}
}
更新!
我已经在Apple Docs中撞了一天左右,然后撞上了我。
class ViewController: UIViewController,AVPlayerItemMetadataOutputPushDelegate {
weak var myDelegate: AVPlayerItemMetadataOutputPushDelegate?
var playerItem: AVPlayerItem?
var player = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
print("lets go!")
let url = URL(string: "<URLREMOVED>")!
let asset = AVAsset(url: url)
let playerItem = AVPlayerItem(asset: asset)
let metadataOutput = AVPlayerItemMetadataOutput(identifiers: nil)
metadataOutput.setDelegate(self, queue: DispatchQueue.main)
playerItem.add(metadataOutput)
player = AVPlayer(playerItem: playerItem)
player.play()
print("END")
print(playerItem.automaticallyLoadedAssetKeys.description)
}
func metadataOutput(_ output: AVPlayerItemMetadataOutput, didOutputTimedMetadataGroups groups: [AVTimedMetadataGroup], from track: AVPlayerItemTrack?) {
}
}
SO,这将读取流以进行定时的元数据更改,然后显示以下输出:
AVMutableMetadataItem:0x600002064020,标识符= icy / StreamTitle,keySpace = icy,密钥类= __NSCFConstantString,key = StreamTitle,commonKey = title,extendedLanguageTag =(null),dataType =(null),time = {102328704/44100 = 2320.379 },持续时间= {1/44100 = 0.000},startDate =(null),额外元素= {\ n},值class = __ NSCFString,值= Tina Turner-世界之路>
好吧?所以,现在我要做的就是将icy / StreamTitle过滤为字符串,我很高兴! :D
答案 0 :(得分:0)
欢迎,看来我已经为我完成了工作! :D Yay学习! 因此,获得上面的AVMutableMetadata东西之后。您需要过滤它,对吧?我以为这会很容易(因为我是新手,大声笑)但是,刚坐下来喝杯茶想一想之后,我为metadataOutput函数做了这个过滤器。
func metadataOutput(_ output: AVPlayerItemMetadataOutput, didOutputTimedMetadataGroups groups: [AVTimedMetadataGroup], from track: AVPlayerItemTrack?) {
let item = groups.first?.items.first // make this an AVMetadata item
item?.value(forKeyPath: "value") // looking for that key bro
print(item!.value(forKeyPath: "value")!) // print the results
}
}
是的,我很高兴自己找到答案。 :D