Preserve playback rate between restarts

This commit is contained in:
Arkadiusz Fal 2022-11-10 23:00:17 +01:00
parent 04a5224ab0
commit a44cce462a
6 changed files with 19 additions and 11 deletions

View File

@ -177,8 +177,8 @@ final class AVPlayerBackend: PlayerBackend {
)
}
func setRate(_ rate: Float) {
avPlayer.rate = rate
func setRate(_ rate: Double) {
avPlayer.rate = Float(rate)
}
func closeItem() {
@ -616,8 +616,8 @@ final class AVPlayerBackend: PlayerBackend {
if player.timeControlStatus == .playing {
self.model.objectWillChange.send()
if player.rate != self.model.currentRate {
player.rate = self.model.currentRate
if player.rate != Float(self.model.currentRate) {
player.rate = Float(self.model.currentRate)
}
}

View File

@ -360,8 +360,8 @@ final class MPVBackend: PlayerBackend {
}
}
func setRate(_ rate: Float) {
client?.setDoubleAsync("speed", Double(rate))
func setRate(_ rate: Double) {
client?.setDoubleAsync("speed", rate)
}
func closeItem() {

View File

@ -47,7 +47,7 @@ protocol PlayerBackend {
func seek(to time: CMTime, seekType: SeekType, completionHandler: ((Bool) -> Void)?)
func seek(to seconds: Double, seekType: SeekType, completionHandler: ((Bool) -> Void)?)
func setRate(_ rate: Float)
func setRate(_ rate: Double)
func closeItem()

View File

@ -47,7 +47,7 @@ final class PlayerModel: ObservableObject {
static var shared: PlayerModel!
static let availableRates: [Float] = [0.5, 0.67, 0.8, 1, 1.25, 1.5, 2]
static let availableRates: [Double] = [0.5, 0.67, 0.8, 1, 1.25, 1.5, 2]
let logger = Logger(label: "stream.yattee.app")
var avPlayerView = AppleAVPlayerView()
@ -86,7 +86,7 @@ final class PlayerModel: ObservableObject {
}}
@Published var aspectRatio = VideoPlayerView.defaultAspectRatio
@Published var stream: Stream?
@Published var currentRate: Float = 1.0 { didSet { backend.setRate(currentRate) } }
@Published var currentRate: Double = 1.0 { didSet { handleCurrentRateChange() } }
@Published var qualityProfileSelection: QualityProfile? { didSet { handleQualityProfileChange() } }
@ -162,6 +162,7 @@ final class PlayerModel: ObservableObject {
@Default(.closePiPOnNavigation) var closePiPOnNavigation
@Default(.closePiPOnOpeningPlayer) var closePiPOnOpeningPlayer
@Default(.resetWatchedStatusOnPlaying) var resetWatchedStatusOnPlaying
@Default(.playerRate) var playerRate
#if !os(macOS)
@Default(.closePiPAndOpenPlayerOnEnteringForeground) var closePiPAndOpenPlayerOnEnteringForeground
@ -197,6 +198,7 @@ final class PlayerModel: ObservableObject {
self.pipDelegate = pipDelegate
pipController?.delegate = pipDelegate
currentRate = playerRate
}
func show() {
@ -540,6 +542,11 @@ final class PlayerModel: ObservableObject {
}
}
func handleCurrentRateChange() {
backend.setRate(currentRate)
playerRate = currentRate
}
func handleQualityProfileChange() {
guard let profile = qualityProfile else { return }
@ -552,7 +559,7 @@ final class PlayerModel: ObservableObject {
}
}
func rateLabel(_ rate: Float) -> String {
func rateLabel(_ rate: Double) -> String {
let formatter = NumberFormatter()
formatter.minimumFractionDigits = 0
formatter.maximumFractionDigits = 2

View File

@ -60,7 +60,7 @@ extension PlayerModel {
private var rateMenuActions: [UIAction] {
PlayerModel.availableRates.map { rate in
let image = currentRate == Float(rate) ? UIImage(systemName: "checkmark") : nil
let image = currentRate == rate ? UIImage(systemName: "checkmark") : nil
return UIAction(title: rateLabel(rate), image: image) { _ in
DispatchQueue.main.async {

View File

@ -87,6 +87,7 @@ extension Defaults.Keys {
static let chargingCellularProfileDefault = hd1080pMPVProfile.id
static let chargingNonCellularProfileDefault = hd1080pMPVProfile.id
#endif
static let playerRate = Key<Double>("playerRate", default: 1.0)
static let qualityProfiles = Key<[QualityProfile]>("qualityProfiles", default: qualityProfilesDefault)
static let batteryCellularProfile = Key<QualityProfile.ID>("batteryCellularProfile", default: batteryCellularProfileDefault)
static let batteryNonCellularProfile = Key<QualityProfile.ID>("batteryNonCellularProfile", default: batteryNonCellularProfileDefault)