Timed Metadata with AVPlayer

前端 未结 2 640
你的背包
你的背包 2021-02-01 11:21

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.

2条回答
  •  心在旅途
    2021-02-01 11:46

    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 {
    
            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.
        }
    }
    

提交回复
热议问题