为什么AVAsset的状态更改观察者不起作用? [英] Why does observer for status change of AVAsset not work?

查看:109
本文介绍了为什么AVAsset的状态更改观察者不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了以下来自苹果的代码.它旨在观察playerItem状态的变化.问题是由于某种原因它不起作用.准备就绪后,观察功能将无法运行.

I implemented the following code from Apple. It is meant to observe for a change in the status of a playerItem. The problem is that for some reason it does not work. The observe function does not run when ready.

所有相关代码如下:

func preloadVideo(media: Media) {
  //setup code, and then:
   media.playerItem1!.addObserver(self,
                                   forKeyPath: #keyPath(AVPlayerItem.status),
                                   options: [.old, .new],
                                   context: &playerItemContext)

}

观察方法:

private var playerItemContext = 0

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    // Only handle observations for the playerItemContext
    print("jdslfhjkfdhaldfahjkflhajfldashkjfdshkjlas")
    guard context == &playerItemContext else {
        super.observeValue(forKeyPath: keyPath, of: object,change: change, context: context)
        return
    }
    
    if keyPath == #keyPath(AVPlayerItem.status) {
        let status: AVPlayerItem.Status
        
        // Get the status change from the change dictionary
        if let statusNumber = change?[.newKey] as? NSNumber {
            status = AVPlayerItem.Status(rawValue: statusNumber.intValue)!
        } else {
            status = .unknown
        }
        print("dsfdgddgdggdgdgdgddsafdsaf434fdfdg433444343")

        // Switch over the status
        switch status {
        case .readyToPlay:
            //Stop animation, and go to p3
            print("dsfdsafdsaf434433444343")

            if goToVideo {
                print("Runinfdsh sahkbdsfhbsadfbadsfl")
                goToP3()
            }
            
            break
        // Player item is ready to play.
        case .failed: break
        // Player item failed. See error.
        case .unknown: break
            // Player item is not yet ready.
        @unknown default: break
            //fatal error
        }
    }
}

此方法将首先运行:

@objc func viewHandleTap(_ sender: UITapGestureRecognizer) {
    if selectedPost?.interimMedia.first?.imageURLString != nil || selectedPost?.interimMedia.first!.playerQueue?.status.rawValue == 1 {
        
        //do something
    } else {
        //status is 0 (video has not loaded yet)//load animation
        //this runs...
        goToVideo = true
    }
}

我如何使其起作用?目前,观察方法中没有任何东西可以打印.

How can I make it work? Currently nothing in the observe method is printing.

如果还有另一种方法可以完成此操作,然后发布答案.

If there is another method to accomplish this go ahead and post as answer.

推荐答案

尝试将addObserver选项更新为[.old,.new,.initial,.prior].

Try by updating addObserver options to [.old, .new, .initial, .prior].

这篇关于为什么AVAsset的状态更改观察者不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆