在我的新SwiftUI项目中,我有一个AVPlayer,用于从url流音乐。现在,我需要通过滑块控制当前的播放时间和音量,这是设计的一部分:
现在,我可以使用UserData最后一个类及其@Published vars来控制播放器,例如isPlaying:
final class UserData: ObservableObject {
// ...
@Published var player: AVPlayer? = nil
@Published var isPlaying: Bool = false
//...
func playPausePlayer(withSong song: Song, forPlaylist playlist: [Song]?) {
//...
if isPlaying {
player?.pause()
} else {
player?.play()
}
isPlaying.toggle()
}
}
很高兴知道这部分是否有更好的决定🧐
问题是属性currentTime,duration我只能从player
或player?.currentItem
中获取,所以不能像这样制作滑块:
@EnvironmentObject var userData: UserData
// ...
Slider(value: userData.player?.currentItem?.currentTime()!, in: 0...userData.player?.currentItem?.duration as! Double, step: 1)
我如何控制这些东西?
我没有找到任何解决方案,因此尝试自行解决。我学了一点Combine
框架,继承了AVPlayer
该类,并在协议ObservableObject
下对其进行了签名,并使用了KVO
。也许这不是最好的解决方案,但它确实有效,希望将来有人能给我一些改进代码的建议。以下是一些代码段:
import Foundation
import AVKit
import Combine
final class AudioPlayer: AVPlayer, ObservableObject {
@Published var currentTimeInSeconds: Double = 0.0
private var timeObserverToken: Any?
// ... some other staff
// MARK: Publishers
var currentTimeInSecondsPass: AnyPublisher<Double, Never> {
return $currentTimeInSeconds
.eraseToAnyPublisher()
}
// in init() method I add observer, which update time in seconds
override init() {
super.init()
registerObserves()
}
private func registerObserves() {
let interval = CMTime(seconds: 1, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
timeObserverToken = self.addPeriodicTimeObserver(forInterval: interval, queue: .main) {
[weak self] _ in
self?.currentTimeInSeconds = self?.currentTime().seconds ?? 0.0
}
}
// func for rewind song time
func rewindTime(to seconds: Double) {
let timeCM = CMTime(seconds: seconds, preferredTimescale: CMTimeScale(NSEC_PER_SEC))
self.seek(to: timeCM)
}
// sure I need to remove observer:
deinit {
if let token = timeObserverToken {
self.removeTimeObserver(token)
timeObserverToken = nil
}
}
}
// simplified slider
import SwiftUI
struct PlayerSlider: View {
@EnvironmentObject var player: AudioPlayer
@State private var currentPlayerTime: Double = 0.0
var song: Song // struct which contains the song length as Int
var body: some View {
HStack {
GeometryReader { geometry in
Slider(value: self.$currentPlayerTime, in: 0.0...Double(self.song.songLength))
.onReceive(self.player.currentTimeInSecondsPass) { _ in
// here I changed the value every second
self.currentPlayerTime = self.player.currentTimeInSeconds
}
// controlling rewind
.gesture(DragGesture(minimumDistance: 0)
.onChanged({ value in
let coefficient = abs(Double(self.song.songLength) / Double(geometry.size.width))
self.player.rewindTime(to: Double(value.location.x) * coefficient)
}))
}
.frame(height: 30)
}
}
}