gpt4 book ai didi

SwiftUI 结合 .onReceive 通知与 AVPlayer

转载 作者:行者123 更新时间:2023-12-05 00:57:15 48 4
gpt4 key购买 nike

我在 SwiftUI 中工作,并且有一个 AudioPlayer 类型,它是 AVPlayer 的子类;它发布 AVPlayer 的 timeControllerStatus(?)(.playing、.paused 和其他?)。我不想继承 AVPlayer,而是想传入一个 AVPlayer 并让它在某些 View 中使用 .onReceive 通知我。这是我目前拥有的功能类型:

import AVKit
import Combine

class AudioPlayer: AVPlayer, ObservableObject {
@Published var buffering: Bool = false

override init() {
super.init()
registerObservers()
}

private func registerObservers() {
self.addObserver(self, forKeyPath: "timeControlStatus", options: [.old, .new], context: nil)
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

if keyPath == "timeControlStatus", let change = change, let newValue = change[NSKeyValueChangeKey.newKey] as? Int, let oldValue = change[NSKeyValueChangeKey.oldKey] as? Int {
let oldStatus = AVPlayer.TimeControlStatus(rawValue: oldValue)
let newStatus = AVPlayer.TimeControlStatus(rawValue: newValue)
if newStatus != oldStatus {
DispatchQueue.main.async {[weak self] in
if newStatus == .playing || newStatus == .paused {
self?.buffering = false
} else {
self?.buffering = true
}
}
}
}
}
}

下面是我想要的类的示例(取自 Chris Mash's tutorial on SwiftUI & AVPlayer):

import Combine
import AVFoundation

class PlayerItemObserver {
let publisher = PassthroughSubject<Bool, Never>()
private var itemObservation: NSKeyValueObservation?

init(player: AVPlayer) {
// Observe the current item changing
itemObservation = player.observe(\.currentItem) { [weak self] player, change in
guard let self = self else { return }
// Publish whether the player has an item or not
self.publisher.send(player.currentItem != nil)
}
}

deinit {
if let observer = itemObservation {
observer.invalidate()
}
}
}

非常感谢您的帮助。

最佳答案

据我了解,您需要像文章示例中一样观察 timeControlStatus。为此,您可以替换观察者:

import Combine
import AVFoundation

class PlayerItemObserver {

let controlStatusChanged = PassthroughSubject<AVPlayer.TimeControlStatus, Never>()
private var itemObservation: NSKeyValueObservation?

init(player: AVPlayer) {

itemObservation = player.observe(\.timeControlStatus) { [weak self] player, change in
guard let self = self else { return }
self.controlStatusChanged.send(player.timeControlStatus)
}

}

deinit {
if let observer = itemObservation {
observer.invalidate()
}
}
}

// MARK: init view
let player = AudioPlayer()
let playerObserver = PlayerItemObserver(player: player)
let contentView = SongListView(playerObserver: playerObserver)

// MARK: react on changing in view:
struct ContentView: View {

let playerObserver: PlayerItemObserver

var body: some View {
Text("Any view")
.onReceive(playerObserver.controlStatusChanged) { newStatus in
switch newStatus {
case .waitingToPlayAtSpecifiedRate:
print("waiting")
case .paused:
print("paused")
case .playing:
print("playing")
}
}
}

}

更新您可以在没有“老派”observe 的情况下使用@PublishedAnyCancellable 实现相同的目的。最后一个偶don't need extra code in deinit .这是这个解决方案:

import Combine
import AVFoundation

class PlayerItemObserver {

@Published var currentStatus: AVPlayer.TimeControlStatus?
private var itemObservation: AnyCancellable?

init(player: AVPlayer) {

itemObservation = player.publisher(for: \.timeControlStatus).sink { newStatus in
self.currentStatus = newStatus
}

}

}

// MARK: you need to change view with new observation, but in general it will be the same
struct ContentView: View {

let playerObserver: PlayerItemObserver

var body: some View {
Text("Any view")
.onReceive(playerObserver.$currentStatus) { newStatus in
switch newStatus {
case nil:
print("nothing is here")
case .waitingToPlayAtSpecifiedRate:
print("waiting")
case .paused:
print("paused")
case .playing:
print("playing")
}
}
}

}

关于SwiftUI 结合 .onReceive 通知与 AVPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60646857/

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