mac/remote: add playback rate info

also rate limit the position to update at max once per second.
This commit is contained in:
der richter 2024-02-08 01:05:25 +01:00
parent e23e158481
commit 0a343369ba
2 changed files with 6 additions and 2 deletions

View File

@ -46,6 +46,7 @@ class RemoteCommandCenter: NSObject {
var isPaused: Bool = false { didSet { updateInfoCenter() } }
var duration: Double = 0 { didSet { updateInfoCenter() } }
var position: Double = 0 { didSet { updateInfoCenter() } }
var rate: Double = 0 { didSet { updateInfoCenter() } }
var infoCenter: MPNowPlayingInfoCenter { get { return MPNowPlayingInfoCenter.default() } }
var commandCenter: MPRemoteCommandCenter { get { return MPRemoteCommandCenter.shared() } }
@ -139,7 +140,7 @@ class RemoteCommandCenter: NSObject {
func updateInfoCenter() {
nowPlayingInfo.merge([
MPNowPlayingInfoPropertyPlaybackRate: NSNumber(value: !isPaused),
MPNowPlayingInfoPropertyPlaybackRate: NSNumber(value: isPaused ? 0 : rate),
MPNowPlayingInfoPropertyElapsedPlaybackTime: NSNumber(value: position),
MPMediaItemPropertyPlaybackDuration: NSNumber(value: duration),
]) { (_, new) in new }
@ -184,11 +185,13 @@ class RemoteCommandCenter: NSObject {
isPaused = LibmpvHelper.mpvFlagToBool(property.data) ?? false
case "time-pos" where property.format == MPV_FORMAT_DOUBLE:
let newPosition = max(LibmpvHelper.mpvDoubleToDouble(property.data) ?? 0, 0)
if Int(floor(newPosition) - floor(position)) != 0 {
if Int((floor(newPosition) - floor(position)) / rate) != 0 {
position = newPosition
}
case "duration" where property.format == MPV_FORMAT_DOUBLE:
duration = LibmpvHelper.mpvDoubleToDouble(property.data) ?? 0
case "speed" where property.format == MPV_FORMAT_DOUBLE:
rate = LibmpvHelper.mpvDoubleToDouble(property.data) ?? 1
default:
break
}

View File

@ -156,6 +156,7 @@ void cocoa_set_mpv_handle(struct mpv_handle *ctx)
if ([[EventsResponder sharedInstance] setMpvHandle:ctx]) {
mpv_observe_property(ctx, 0, "duration", MPV_FORMAT_DOUBLE);
mpv_observe_property(ctx, 0, "time-pos", MPV_FORMAT_DOUBLE);
mpv_observe_property(ctx, 0, "speed", MPV_FORMAT_DOUBLE);
mpv_observe_property(ctx, 0, "pause", MPV_FORMAT_FLAG);
mpv_set_wakeup_callback(ctx, wakeup, NULL);
}