1
1
mirror of https://github.com/danbee/persephone synced 2025-03-04 08:39:11 +00:00
persephone/Mac/Components/Shared/PlayerStateInfoController.swift
Alan Harper a0f1942ba8 Use MPNowPlayingInfoCenter to player info
This means that the notications bar media player will show track &
artist title, as well as artwork for the album.

It also means that Play/Pause/Next/Previous work without needing
accessibility access
2021-06-19 17:39:09 -04:00

73 lines
1.6 KiB
Swift

//
// MediaInfoController.swift
// Persephone
//
// Created by Alan Harper on 18/11/20.
// Copyright © 2020 Dan Barber. All rights reserved.
//
import Foundation
import ReSwift
import MediaPlayer
import Kingfisher
class PlayerStateInfoController {
init() {
App.store.subscribe(self) {
$0.select { $0.playerState.state }
}
let commandCenter = MPRemoteCommandCenter.shared()
commandCenter.playCommand.addTarget { _ in
App.mpdClient.playPause()
return .success
}
commandCenter.togglePlayPauseCommand.addTarget { _ in
App.mpdClient.playPause()
return .success
}
commandCenter.pauseCommand.addTarget { _ in
App.mpdClient.playPause()
return .success
}
commandCenter.nextTrackCommand.addTarget { _ in
App.mpdClient.nextTrack()
return .success
}
commandCenter.previousTrackCommand.addTarget { _ in
App.mpdClient.prevTrack()
return .success
}
}
func notifyState(_ state: MPDClient.MPDStatus.State?) {
let infoCenter = MPNowPlayingInfoCenter.default()
switch(state) {
case .unknown:
infoCenter.playbackState = .unknown
break;
case .paused:
infoCenter.playbackState = .paused
break;
case .stopped:
infoCenter.playbackState = .stopped
break;
case .playing:
infoCenter.playbackState = .playing
break;
case .none:
return
}
}
}
extension PlayerStateInfoController: StoreSubscriber {
typealias StoreSubscriberStateType = MPDClient.MPDStatus.State?
func newState(state: StoreSubscriberStateType) {
notifyState(state)
}
}