在 SwiftUI 中监听 AVPlayer timeControlStatus 的变化
Listen to AVPlayer timeControlStatus changes in SwiftUI
因此尝试在 SwiftUI 中实现 AVPlayer 的简单观察者。根据 Apple 文档,我已经添加了被触发但还没有想出 how/where 来实现 observeValue
的观察者:https://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing/responding_to_playback_state_changes
到目前为止,我的 AVPlayer class 如下:
class Player: AVPlayer, ObservableObject {
@Published var isPlaying: Bool = false
static var shared = AVPlayer()
static var episodeId: Int?
static func playItem(at itemURL: String, episodeId: Int) {
let url = URL(string: itemURL)
Player.shared = AVPlayer(url: url!)
Player.episodeId = episodeId
Player.shared.addObserver(self.shared, forKeyPath: "timeControlStatus", options: [.old, .new], context: nil)
}
}
所以问题是我应该在哪里实现它才能工作:
override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?) {
if Player.shared.timeControlStatus == .playing {
// Set @Published isPlaying to true
}
}
它可以像下面这样(不要使用static
在这种情况下不需要,无论如何你将需要实例在@ObservedObject
中使用)
当然不是最终的Player
,但进化的方向应该是明确的:
class Player: AVPlayer, ObservableObject {
@Published var isPlaying: Bool = false
private var playerContext = 0
var player: AVPlayer? = nil
var episodeId: Int?
func playItem(at itemURL: String, episodeId: Int) {
guard let url = URL(string: itemURL) else { return }
// cleanup for previous player
self.player?.removeObserver(self, forKeyPath: "timeControlStatus")
// setup new player
let newPlayer = AVPlayer(url: url)
newPlayer.addObserver(self, forKeyPath: "timeControlStatus", options: [.old, .new], context: &playerContext)
self.player = newPlayer
self.episodeId = episodeId
}
override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?) {
guard context == &playerContext else { // give super to handle own cases
super.observeValue(forKeyPath: keyPath,
of: object,
change: change,
context: context)
return
}
if self.player?.timeControlStatus == .playing {
self.isPlaying = true
}
}
}
因此尝试在 SwiftUI 中实现 AVPlayer 的简单观察者。根据 Apple 文档,我已经添加了被触发但还没有想出 how/where 来实现 observeValue
的观察者:https://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing/responding_to_playback_state_changes
到目前为止,我的 AVPlayer class 如下:
class Player: AVPlayer, ObservableObject {
@Published var isPlaying: Bool = false
static var shared = AVPlayer()
static var episodeId: Int?
static func playItem(at itemURL: String, episodeId: Int) {
let url = URL(string: itemURL)
Player.shared = AVPlayer(url: url!)
Player.episodeId = episodeId
Player.shared.addObserver(self.shared, forKeyPath: "timeControlStatus", options: [.old, .new], context: nil)
}
}
所以问题是我应该在哪里实现它才能工作:
override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?) {
if Player.shared.timeControlStatus == .playing {
// Set @Published isPlaying to true
}
}
它可以像下面这样(不要使用static
在这种情况下不需要,无论如何你将需要实例在@ObservedObject
中使用)
当然不是最终的Player
,但进化的方向应该是明确的:
class Player: AVPlayer, ObservableObject {
@Published var isPlaying: Bool = false
private var playerContext = 0
var player: AVPlayer? = nil
var episodeId: Int?
func playItem(at itemURL: String, episodeId: Int) {
guard let url = URL(string: itemURL) else { return }
// cleanup for previous player
self.player?.removeObserver(self, forKeyPath: "timeControlStatus")
// setup new player
let newPlayer = AVPlayer(url: url)
newPlayer.addObserver(self, forKeyPath: "timeControlStatus", options: [.old, .new], context: &playerContext)
self.player = newPlayer
self.episodeId = episodeId
}
override func observeValue(forKeyPath keyPath: String?,
of object: Any?,
change: [NSKeyValueChangeKey : Any]?,
context: UnsafeMutableRawPointer?) {
guard context == &playerContext else { // give super to handle own cases
super.observeValue(forKeyPath: keyPath,
of: object,
change: change,
context: context)
return
}
if self.player?.timeControlStatus == .playing {
self.isPlaying = true
}
}
}