Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode AVPlayer状态:消息已收到但未处理_Xcode_Swift_Avplayer_Addobserver - Fatal编程技术网

Xcode AVPlayer状态:消息已收到但未处理

Xcode AVPlayer状态:消息已收到但未处理,xcode,swift,avplayer,addobserver,Xcode,Swift,Avplayer,Addobserver,我对Observators和Swift/ObjC非常陌生,我正试图使用苹果文档中所述的方法来监控AVPlayer的状态,但当我尝试添加Observator时,我得到的是一条“消息已收到但未处理”错误消息 gs_mediaObjAdv=AVPlayer(URL: NSURL(string: mediaURL)); gs_mediaObjAdv.addObserver(self, forKeyPath: "status", options:NSKeyValueObservingOptions.New

我对Observators和Swift/ObjC非常陌生,我正试图使用苹果文档中所述的方法来监控AVPlayer的状态,但当我尝试添加Observator时,我得到的是一条“消息已收到但未处理”错误消息

gs_mediaObjAdv=AVPlayer(URL: NSURL(string: mediaURL));
gs_mediaObjAdv.addObserver(self, forKeyPath: "status", options:NSKeyValueObservingOptions.New, context:nil);
如果我取消注册观察者,错误将消失,但是如果我在observeValueForKeyPath函数中添加println(“test”),则不会发生任何事情


您知道如何以简单的方式监控状态吗?

请检查您有覆盖的观察者方法。
它必须是实例方法,而不是类方法。

请检查是否有覆盖的Observer方法。
它必须是一个实例方法而不是类方法。

下面是其他需要它的人的答案 Swift 5.1

//playerViewController is defined as a class member 
//let playerViewController = AVPlayerViewController()
self.playerViewController.player!.addObserver(self, forKeyPath: "rate", options: .new, context: nil)

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "rate" {
        if let rate = change?[NSKeyValueChangeKey.newKey] as? Float {
            if rate == 0.0 {
                print("playback stopped")
                //view.addSubview(drawView)
            }
            if rate == 1.0 {
                print("normal playback")
            }
            if rate == -1.0 {
                print("reverse playback")
            }
        }
    }
    super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}

这是其他需要它的人的答案 Swift 5.1

//playerViewController is defined as a class member 
//let playerViewController = AVPlayerViewController()
self.playerViewController.player!.addObserver(self, forKeyPath: "rate", options: .new, context: nil)

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "rate" {
        if let rate = change?[NSKeyValueChangeKey.newKey] as? Float {
            if rate == 0.0 {
                print("playback stopped")
                //view.addSubview(drawView)
            }
            if rate == 1.0 {
                print("normal playback")
            }
            if rate == -1.0 {
                print("reverse playback")
            }
        }
    }
    super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}

什么是您的
observeValueForKeyPath
code您是否进行了超级调用,例如
[super observeValueForKeyPath:keyPath of object:object change:change context:context]?我认为,如果您的super不兼容KVO,则可能存在此问题。试着对代码进行注释。你找到解决方案了吗?我也有同样的问题。你的
observeValueForKeyPath
代码是什么?你是否做了一个超级调用,比如
[super observeValueForKeyPath:keyPath of object:object change:change context:context]?我认为,如果您的super不兼容KVO,则可能存在此问题。试着对代码进行注释。你找到解决方案了吗?我也有同样的问题。那正是我的问题:那正是我的问题: