Nicholas Young Nicholas Young - 7 months ago 233
Swift Question

Timed Metadata with AVPlayer

Can you control how

AVURLAsset
loads the data it requires? I need to add a custom HTTP header (
ICY-MetaData=1
) and can't seem to figure out how.

Answer

Apparently, AVPlayer automatically requests metadata from Icecast. The code below works perfectly.

class ViewController: UIViewController {
    var Player: AVPlayer!
    var PlayerItem: AVPlayerItem!

    override func viewDidLoad() {
        super.viewDidLoad()

        PlayerItem = AVPlayerItem(URL: NSURL(string: "http://live.machine.fm/aac"))
        PlayerItem.addObserver(self, forKeyPath: "timedMetadata", options: nil, context: nil)
        Player = AVPlayer(playerItem: PlayerItem)
        Player.play()
    }

    override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) -> Void {

        if keyPath != "timedMetadata" { return }

        var data: AVPlayerItem = object as AVPlayerItem

        for item in data.timedMetadata as [AVMetadataItem] {
            println(item.value)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
Comments