我想从MP3 HTTP流中获取元数据。使用AVPlayer播放流。
我已尝试使用AVPlayerItemMetadataCollector,但它返回nil。
另一种方法是在显示当前标题的流网站上获取特定部分。
这是我的播放器代码:
let playerItem = AVPlayerItem(url: URL(string: "stream.p4.no/p5trondheim_mp3_hq")!)
player = AVPlayer(playerItem: playerItem)
player.rate = 1.0;
player.play()
我该怎么办?
答案 0 :(得分:1)
您可以在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)'")
}
}
}