如何检测AVPlayer swift中的错误链接播放

时间:2016-08-30 17:34:46

标签: ios swift avplayer avplayerviewcontroller

我在我的项目中使用AVPlayer来播放视频流。我的项目是用Swift语言编写的。

如何在AVPlayer

中检测到错误的链接

我用过这个:

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

但结果是它总是返回:

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

任何提示都会有所帮助。感谢。

2 个答案:

答案 0 :(得分:1)

嗯,我还没有测试过,但试试这个。

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")
            }

        }
    }
}

答案 1 :(得分:0)

我已经检查过了。

您可以为您的播放器添加它。

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))")
        }
    }
}

希望这会对您有所帮助。