4

I'm using AVPlayer in my project to play video streaming. My project was written in Swift language.

How can I detect that wrong link is played in AVPlayer?

I used this:

player.addObserver(self, forKeyPath: "status", options:NSKeyValueObservingOptions(), context: nil)

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if (keyPath == "status") {
            let status: AVPlayerStatus = self.playerViewController.player!.status
            switch (status) {
            case AVPlayerStatus.ReadyToPlay:
                print("---------- ReadyToPlay ----------")
                break
            case AVPlayerStatus.Unknown, AVPlayerStatus.Failed:
                print("---------- FAILED ----------")
                break
            }
        }
    }

but the result is that it always returns:

---------- ReadyToPlay ----------

Any hints will be helpful. Thanks.

EugZol
  • 6,476
  • 22
  • 41
Nguyen Hung
  • 113
  • 2
  • 7
  • Your code, while not very idiomatic, looks correctly structured. What's the problem? – matt Aug 30 '16 at 17:45
  • I tried a error link stream but it is still show ---------- ReadyToPlay ---------- :-( – MrSiro Aug 31 '16 at 07:17
  • @MrSiro Did you find a solution for this? I have the same status, after providing a broken link to the player, status still is ready to play. – Aivars Nov 23 '18 at 14:24

2 Answers2

2

I had checked about this.

you can add this for your player.

player.addObserver(self, forKeyPath: "timeControlStatus", options:[.new, .old], context:nil)
player.addObserver(self, forKeyPath: #keyPath(AVPlayer.currentItem.status), options:[.new, .old], context:nil)


override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    
    if keyPath == "timeControlStatus", let change = change, let newValue = change[NSKeyValueChangeKey.newKey] as? Int, let oldValue = change[NSKeyValueChangeKey.oldKey] as? Int {
        let oldStatus = AVPlayer.TimeControlStatus(rawValue: oldValue)
        let newStatus = AVPlayer.TimeControlStatus(rawValue: newValue)
        if newStatus != oldStatus {
            DispatchQueue.main.async {
                if newStatus == .playing || newStatus == .paused {
                    //playing
                } else {
                    //loading video
                }
            }
        }
    }
    
    if keyPath == #keyPath(AVPlayer.currentItem.status) {
        let newStatus: AVPlayerItem.Status
        if let newStatusAsNumber = change?[NSKeyValueChangeKey.newKey] as? NSNumber {
            newStatus = AVPlayerItem.Status(rawValue: newStatusAsNumber.intValue)!
        } else {
            newStatus = .unknown
        }
        if newStatus == .failed {
            // this is just error you want to handle
            print("Error: \(String(describing: self.player?.currentItem?.error?.localizedDescription)), error: \(String(describing: self.player?.currentItem?.error))")
        }
    }
}

hope this will help you.

Guru Dev
  • 171
  • 11
1

Hmm I havent tested it yet but try this.

player?.addObserver(self, forKeyPath: "status", options: .new, context: nil)
}



override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if (keyPath == "status") {
        let status: AVPlayerStatus = self.player!.status
        if(status == AVPlayerStatus.readyToPlay){
            print("Ready to play")
        }
        else{
            if( status == AVPlayerStatus.unknown){
                print("failed")
            }

        }
    }
}
Sadin
  • 25
  • 7