gpt4 book ai didi

ios - 当应用程序进入前台时,AVPlayer 状态会更新

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

我正在使用 AVPlayer 在我的 iOS 应用程序中构建一个音乐播放器。我像这样监听 AVPlayer.status 属性的变化,以了解何时可以播放音频:

player.currentItem!.addObserver(self, forKeyPath: "status", options: .New, context: nil)

当状态为 .ReadyToPlay 时,我会自动开始播放:

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if (keyPath == "status") {
if let currentItem = self.player?.currentItem {
let status = currentItem.status
if (status == .ReadyToPlay) {
self.play()
}
}
}
}
}

效果很好。然而,问题是,如果我开始在我的应用程序中播放音乐,暂停音乐然后离开应用程序并开始播放音乐,例如 Spotify,AVPlayer 的状态属性似乎下次我的应用进入前台时再次更改为 .ReadyToPlay,这会导致观察者触发,进而导致音乐再次开始播放。

我假设当应用程序再次获得焦点时 AVPlayer 实例发生了一些事情,这导致状态属性更改/刷新。

如何防止这种行为?

最佳答案

这似乎是预期的行为。如果要确保仅在 AVPlayerItem 状态更改后第一次开始播放,请在调用 play() 后移除观察器。

这里需要注意的是,当播放器上的 currentItem 更改时,您应该已经移除了观察者,因此您需要使用一个额外的标志来跟踪您是否正在观察现有的 当前项目

播放器的所有者会跟踪状态

var isObservingCurrentItem = false

并在添加观察者时更新/检查该状态

if currentItem = player.currentItem where isObservingCurrentItem {
currentItem.removeObserver(self, forKeyPath:"status")
}

player.currentItem!.addObserver(self, forKeyPath: "status", options: .New, context: nil)
isObservingCurrentItem = true

一旦玩家准备好开始游戏,您就可以安全地移除观察者

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {

if let object = object,
keyPath = keyPath,
currentItem = self.player?.currentItem,
status = currentItem.status
where status == .ReadyToPlay {
self.play()
object.removeObserver(self, forKeyPath:keyPath)
isObservingCurrentItem = false
}
}

关于ios - 当应用程序进入前台时,AVPlayer 状态会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33952870/

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