Nicholas Young Nicholas Young - 1 year ago 441
Swift Question

Timed Metadata with AVPlayer

Can you control how

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

Answer Source

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

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

    override func viewDidLoad() {

        PlayerItem = AVPlayerItem(URL: NSURL(string: ""))
        PlayerItem.addObserver(self, forKeyPath: "timedMetadata", options: nil, context: nil)
        Player = AVPlayer(playerItem: PlayerItem)

    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] {

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