当应用程序进入前台时,AVPlayer 状态会更新
AVPlayer status is updated when app comes to foreground
我正在使用 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 实例发生了一些事情,这导致状态 属性 变为 change/refresh。
如何防止这种行为?
这似乎是预期的行为。如果要确保仅在 AVPlayerItem
状态更改后第一次开始播放,请在调用 play()
.
后删除观察者
这里需要注意的是,当播放器上的 currentItem
发生变化时,您应该已经移除了观察者,因此您需要使用一个额外的标志来跟踪您是否在观察现有的 [=15] =].
播放器的所有者会跟踪状态
var isObservingCurrentItem = false
和 update/check 添加观察者时的状态
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
}
}
我正在使用 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 实例发生了一些事情,这导致状态 属性 变为 change/refresh。
如何防止这种行为?
这似乎是预期的行为。如果要确保仅在 AVPlayerItem
状态更改后第一次开始播放,请在调用 play()
.
这里需要注意的是,当播放器上的 currentItem
发生变化时,您应该已经移除了观察者,因此您需要使用一个额外的标志来跟踪您是否在观察现有的 [=15] =].
播放器的所有者会跟踪状态
var isObservingCurrentItem = false
和 update/check 添加观察者时的状态
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
}
}