mirror of https://github.com/mpv-player/mpv
mac: report playback state to the MediaPlayer Command Center
some system logic for the global media key events depends on the right playback state. set the state properly to prevent more breakages in the future.
This commit is contained in:
parent
3d16ab1a31
commit
695d850412
|
@ -87,7 +87,10 @@ class RemoteCommandCenter: NSObject {
|
|||
MPRemoteCommandCenter.shared().bookmarkCommand,
|
||||
]
|
||||
|
||||
let application: Application;
|
||||
let application: Application
|
||||
|
||||
var mpInfoCenter: MPNowPlayingInfoCenter { get { return MPNowPlayingInfoCenter.default() } }
|
||||
var isPaused: Bool = false { didSet { updatePlaybackState() } }
|
||||
|
||||
@objc init(app: Application) {
|
||||
application = app
|
||||
|
@ -99,11 +102,6 @@ class RemoteCommandCenter: NSObject {
|
|||
}
|
||||
}
|
||||
|
||||
@objc func makeCurrent() {
|
||||
MPNowPlayingInfoCenter.default().playbackState = .paused
|
||||
MPNowPlayingInfoCenter.default().playbackState = .playing
|
||||
}
|
||||
|
||||
@objc func start() {
|
||||
for (cmd, _) in config {
|
||||
cmd.isEnabled = true
|
||||
|
@ -119,8 +117,8 @@ class RemoteCommandCenter: NSObject {
|
|||
nowPlayingInfo[MPMediaItemPropertyArtwork] = albumArt
|
||||
}
|
||||
|
||||
MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo
|
||||
MPNowPlayingInfoCenter.default().playbackState = .playing
|
||||
mpInfoCenter.nowPlayingInfo = nowPlayingInfo
|
||||
mpInfoCenter.playbackState = .playing
|
||||
}
|
||||
|
||||
@objc func stop() {
|
||||
|
@ -129,8 +127,18 @@ class RemoteCommandCenter: NSObject {
|
|||
cmd.removeTarget(nil)
|
||||
}
|
||||
|
||||
MPNowPlayingInfoCenter.default().nowPlayingInfo = nil
|
||||
MPNowPlayingInfoCenter.default().playbackState = .unknown
|
||||
mpInfoCenter.nowPlayingInfo = nil
|
||||
mpInfoCenter.playbackState = .unknown
|
||||
}
|
||||
|
||||
@objc func makeCurrent() {
|
||||
mpInfoCenter.playbackState = .paused
|
||||
mpInfoCenter.playbackState = .playing
|
||||
updatePlaybackState()
|
||||
}
|
||||
|
||||
func updatePlaybackState() {
|
||||
mpInfoCenter.playbackState = isPaused ? .paused : .playing
|
||||
}
|
||||
|
||||
func cmdHandler(_ event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus {
|
||||
|
@ -156,4 +164,27 @@ class RemoteCommandCenter: NSObject {
|
|||
|
||||
return .success
|
||||
}
|
||||
}
|
||||
|
||||
@objc func processEvent(_ event: UnsafeMutablePointer<mpv_event>) {
|
||||
switch event.pointee.event_id {
|
||||
case MPV_EVENT_PROPERTY_CHANGE:
|
||||
handlePropertyChange(event)
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
func handlePropertyChange(_ event: UnsafeMutablePointer<mpv_event>) {
|
||||
let pData = OpaquePointer(event.pointee.data)
|
||||
guard let property = UnsafePointer<mpv_event_property>(pData)?.pointee else {
|
||||
return
|
||||
}
|
||||
|
||||
switch String(cString: property.name) {
|
||||
case "pause" where property.format == MPV_FORMAT_FLAG:
|
||||
isPaused = LibmpvHelper.mpvFlagToBool(property.data) ?? false
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -173,6 +173,12 @@ static const char macosx_icon[] =
|
|||
#if HAVE_MACOS_TOUCHBAR
|
||||
if ([self respondsToSelector:@selector(touchBar)])
|
||||
[(TouchBar *)self.touchBar processEvent:event];
|
||||
#endif
|
||||
#if HAVE_MACOS_MEDIA_PLAYER
|
||||
// 10.12.2 runtime availability check
|
||||
if ([self respondsToSelector:@selector(touchBar)]) {
|
||||
[_remoteCommandCenter processEvent:event];
|
||||
}
|
||||
#endif
|
||||
if (_cocoa_cb) {
|
||||
[_cocoa_cb processEvent:event];
|
||||
|
|
Loading…
Reference in New Issue