mirror of
https://github.com/yattee/yattee.git
synced 2024-11-12 17:18:22 +00:00
updateNowPlayingInfo also with System controls
Signed-off-by: Toni Förster <toni.foerster@gmail.com>
This commit is contained in:
parent
1a22ac71be
commit
740a2f85ac
@ -880,26 +880,29 @@ final class PlayerModel: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateRemoteCommandCenter() {
|
func updateRemoteCommandCenter() {
|
||||||
let skipForwardCommand = MPRemoteCommandCenter.shared().skipForwardCommand
|
let commandCenter = MPRemoteCommandCenter.shared()
|
||||||
let skipBackwardCommand = MPRemoteCommandCenter.shared().skipBackwardCommand
|
let skipForwardCommand = commandCenter.skipForwardCommand
|
||||||
let previousTrackCommand = MPRemoteCommandCenter.shared().previousTrackCommand
|
let skipBackwardCommand = commandCenter.skipBackwardCommand
|
||||||
let nextTrackCommand = MPRemoteCommandCenter.shared().nextTrackCommand
|
let previousTrackCommand = commandCenter.previousTrackCommand
|
||||||
|
let nextTrackCommand = commandCenter.nextTrackCommand
|
||||||
|
|
||||||
if !remoteCommandCenterConfigured {
|
if !remoteCommandCenterConfigured {
|
||||||
remoteCommandCenterConfigured = true
|
remoteCommandCenterConfigured = true
|
||||||
|
|
||||||
#if !os(macOS)
|
|
||||||
try? AVAudioSession.sharedInstance().setCategory(
|
|
||||||
.playback,
|
|
||||||
mode: .moviePlayback
|
|
||||||
)
|
|
||||||
|
|
||||||
UIApplication.shared.beginReceivingRemoteControlEvents()
|
|
||||||
#endif
|
|
||||||
|
|
||||||
let interval = TimeInterval(systemControlsSeekDuration) ?? 10
|
let interval = TimeInterval(systemControlsSeekDuration) ?? 10
|
||||||
let preferredIntervals = [NSNumber(value: interval)]
|
let preferredIntervals = [NSNumber(value: interval)]
|
||||||
|
|
||||||
|
// Remove existing targets to avoid duplicates
|
||||||
|
skipForwardCommand.removeTarget(nil)
|
||||||
|
skipBackwardCommand.removeTarget(nil)
|
||||||
|
previousTrackCommand.removeTarget(nil)
|
||||||
|
nextTrackCommand.removeTarget(nil)
|
||||||
|
commandCenter.playCommand.removeTarget(nil)
|
||||||
|
commandCenter.pauseCommand.removeTarget(nil)
|
||||||
|
commandCenter.togglePlayPauseCommand.removeTarget(nil)
|
||||||
|
commandCenter.changePlaybackPositionCommand.removeTarget(nil)
|
||||||
|
|
||||||
|
// Re-add targets for handling commands
|
||||||
skipForwardCommand.preferredIntervals = preferredIntervals
|
skipForwardCommand.preferredIntervals = preferredIntervals
|
||||||
skipBackwardCommand.preferredIntervals = preferredIntervals
|
skipBackwardCommand.preferredIntervals = preferredIntervals
|
||||||
|
|
||||||
@ -923,22 +926,22 @@ final class PlayerModel: ObservableObject {
|
|||||||
return .success
|
return .success
|
||||||
}
|
}
|
||||||
|
|
||||||
MPRemoteCommandCenter.shared().playCommand.addTarget { [weak self] _ in
|
commandCenter.playCommand.addTarget { [weak self] _ in
|
||||||
self?.play()
|
self?.play()
|
||||||
return .success
|
return .success
|
||||||
}
|
}
|
||||||
|
|
||||||
MPRemoteCommandCenter.shared().pauseCommand.addTarget { [weak self] _ in
|
commandCenter.pauseCommand.addTarget { [weak self] _ in
|
||||||
self?.pause()
|
self?.pause()
|
||||||
return .success
|
return .success
|
||||||
}
|
}
|
||||||
|
|
||||||
MPRemoteCommandCenter.shared().togglePlayPauseCommand.addTarget { [weak self] _ in
|
commandCenter.togglePlayPauseCommand.addTarget { [weak self] _ in
|
||||||
self?.togglePlay()
|
self?.togglePlay()
|
||||||
return .success
|
return .success
|
||||||
}
|
}
|
||||||
|
|
||||||
MPRemoteCommandCenter.shared().changePlaybackPositionCommand.addTarget { [weak self] remoteEvent in
|
commandCenter.changePlaybackPositionCommand.addTarget { [weak self] remoteEvent in
|
||||||
guard let event = remoteEvent as? MPChangePlaybackPositionCommandEvent else { return .commandFailed }
|
guard let event = remoteEvent as? MPChangePlaybackPositionCommandEvent else { return .commandFailed }
|
||||||
|
|
||||||
self?.backend.seek(to: event.positionTime, seekType: .userInteracted)
|
self?.backend.seek(to: event.positionTime, seekType: .userInteracted)
|
||||||
@ -1017,18 +1020,22 @@ final class PlayerModel: ObservableObject {
|
|||||||
guard activeBackend == .mpv else { return }
|
guard activeBackend == .mpv else { return }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if os(iOS)
|
|
||||||
if activeBackend == .appleAVPlayer, avPlayerUsesSystemControls {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
guard let video = currentItem?.video else {
|
guard let video = currentItem?.video else {
|
||||||
MPNowPlayingInfoCenter.default().nowPlayingInfo = .none
|
MPNowPlayingInfoCenter.default().nowPlayingInfo = .none
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let currentTime = (backend.currentTime?.seconds.isFinite ?? false) ? backend.currentTime!.seconds : 0
|
let currentTime = (backend.currentTime?.seconds.isFinite ?? false) ? backend.currentTime!.seconds : 0
|
||||||
|
|
||||||
|
// Determine the media type based on musicMode
|
||||||
|
let mediaType: NSNumber
|
||||||
|
if musicMode {
|
||||||
|
mediaType = MPMediaType.anyAudio.rawValue as NSNumber
|
||||||
|
} else {
|
||||||
|
mediaType = MPMediaType.anyVideo.rawValue as NSNumber
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare the Now Playing info dictionary
|
||||||
var nowPlayingInfo: [String: AnyObject] = [
|
var nowPlayingInfo: [String: AnyObject] = [
|
||||||
MPMediaItemPropertyTitle: video.displayTitle as AnyObject,
|
MPMediaItemPropertyTitle: video.displayTitle as AnyObject,
|
||||||
MPMediaItemPropertyArtist: video.displayAuthor as AnyObject,
|
MPMediaItemPropertyArtist: video.displayAuthor as AnyObject,
|
||||||
@ -1036,7 +1043,7 @@ final class PlayerModel: ObservableObject {
|
|||||||
MPNowPlayingInfoPropertyElapsedPlaybackTime: currentTime as AnyObject,
|
MPNowPlayingInfoPropertyElapsedPlaybackTime: currentTime as AnyObject,
|
||||||
MPNowPlayingInfoPropertyPlaybackQueueCount: queue.count as AnyObject,
|
MPNowPlayingInfoPropertyPlaybackQueueCount: queue.count as AnyObject,
|
||||||
MPNowPlayingInfoPropertyPlaybackQueueIndex: 1 as AnyObject,
|
MPNowPlayingInfoPropertyPlaybackQueueIndex: 1 as AnyObject,
|
||||||
MPMediaItemPropertyMediaType: MPMediaType.anyVideo.rawValue as AnyObject
|
MPMediaItemPropertyMediaType: mediaType
|
||||||
]
|
]
|
||||||
|
|
||||||
if !currentArtwork.isNil {
|
if !currentArtwork.isNil {
|
||||||
@ -1240,7 +1247,7 @@ final class PlayerModel: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func destroyKeyPressMonitor() {
|
private func destroyKeyPressMonitor() {
|
||||||
if let keyPressMonitor = keyPressMonitor {
|
if let keyPressMonitor {
|
||||||
NSEvent.removeMonitor(keyPressMonitor)
|
NSEvent.removeMonitor(keyPressMonitor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user