gpt4 book ai didi

ios - 在 SwiftUI 中监听 AVPlayer timeControlStatus 的变化

转载 作者:行者123 更新时间:2023-12-04 17:30:19 25 4
gpt4 key购买 nike

因此尝试在 SwiftUI 中实现 AVPlayer 的简单观察者。我已经添加了被触发的观察者,但还没有根据 Apple 文档弄清楚如何/在哪里实现 observeValue:https://developer.apple.com/documentation/avfoundation/media_assets_playback_and_editing/responding_to_playback_state_changes

到目前为止,我的 AVPlayer 类如下:

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

关于ios - 在 SwiftUI 中监听 AVPlayer timeControlStatus 的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60361850/

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