Receiving metadata from .pls in Swift 2 Xcode 7

こ雲淡風輕ζ 提交于 2019-12-12 01:28:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!