我想从MP3 HTTP流中获取元数据流是用AVPlayer播放的。
我用AVPlayerItemMetadataCollector尝试过,但它返回零。
另一种方法是在显示当前标题的流的网站上获取特定部分。
这是我的玩家代码:
let playerItem = AVPlayerItem(url: URL(string: "stream.p4.no/p5trondheim_mp3_hq")!)
player = AVPlayer(playerItem: playerItem)
player.rate = 1.0;
player.play()
我能做什么?
最佳答案
您可以在AVPlayerItem上观察timedMetadata:
{
...
// Add observer
playerItem.addObserver(self, forKeyPath: "timedMetadata", options: .new, context: nil)
...
}
// Observe
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard keyPath == "timedMetadata" else { return }
guard let meta = playerItem.timedMetadata else { return }
for metadata in meta {
if let songName = metadata.value(forKey: "value") as? String {
print("song name is '\(songName)'")
}
}
}