Timed Metadata with AVPlayer

前端 未结 2 642
你的背包
你的背包 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:39

    Nicolas Young's answer is correct it just needs some small changes to work with Swift 3. There were some red exclamation points so now that I understand key value observation a little better I finally figured out how to fix this

    import UIKit
    import MediaPlayer
    
    //from the original
    //https://stackoverflow.com/a/28057734/1839484
    
    class ViewController: UIViewController {
    var Player: AVPlayer!
    var PlayerItem: AVPlayerItem!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let playBackURL = URL(string: "http://ca2.rcast.net:8044/")
        PlayerItem = AVPlayerItem(url: playBackURL!)
        PlayerItem.addObserver(self, forKeyPath: "timedMetadata", options: NSKeyValueObservingOptions(), context: nil)
        Player = AVPlayer(playerItem: PlayerItem)
        Player.play()
    }
    
    
    override func observeValue(forKeyPath: String?, of: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if forKeyPath != "timedMetadata" { return }
    
        let data: AVPlayerItem = of as! AVPlayerItem
    
        for item in data.timedMetadata! {
            print(item.value!)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    }
    

提交回复
热议问题