本文介绍了在Swift 2 Xcode 7中从.pls接收元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个无线电应用程序,并希望从.pls格式的网址和以下代码中获取元数据:
I'm writing an radio app and want to get metadata from url of .pls format and the following code:
var PlayerItem: AVPlayerItem!
var asset : AVAsset? // init with url of .pls format
PlayerItem = AVPlayerItem(asset: asset!)
PlayerItem.addObserver(self, forKeyPath: "timedMetadata" [...]
PlayerItem.timedMetadata - is always nil or 0
我应该添加些什么来接收元数据?
What am I supposed to add, to receive metadatas?
推荐答案
快速Swift 2实现:
Quick Swift 2 implemetation:
var player: AVPlayer!
var playerItem: AVPlayerItem!
override func viewDidLoad() {
super.viewDidLoad()
....
playerItem = AVPlayerItem(URL: NSURL(string: <streamURL>)!)
player = AVPlayer(playerItem: playerItem)
// To be informed when metadata changes
playerItem.addObserver(self, forKeyPath: "timedMetadata", options: [], context: nil)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
// Get medatata, making sure to support a wider range of characters
let origMetaTitle = (playerItem.timedMetadata?.first?.stringValue?.dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: true))!
let convertedMetaTitle = String(data: origMeta, encoding: NSUTF8StringEncoding)!
....
}
deinit {
// Be a good citizen
playerItem.removeObserver(self, forKeyPath: "timedMetadata")
}
这篇关于在Swift 2 Xcode 7中从.pls接收元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!