问题
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?
回答1:
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")
}
来源:https://stackoverflow.com/questions/33660736/receiving-metadata-from-pls-in-swift-2-xcode-7