gpt4 book ai didi

ios - 为什么 AVAsset 状态变化的观察者不起作用?

转载 作者:搜寻专家 更新时间:2023-10-31 22:53:08 24 4
gpt4 key购买 nike

我实现了以下 code from Apple .它旨在观察 playerItem 状态的变化。问题是由于某种原因它不起作用。观察函数在就绪时不会运行。

所有相关代码如下:

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

我怎样才能让它发挥作用?目前 observe 方法中没有任何内容正在打印。

如果有其他方法可以完成此操作,请继续并作为答案发布。

最佳答案

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

关于ios - 为什么 AVAsset 状态变化的观察者不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56470171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com