forked from Blendman974/faceclaw
Add an iOS Spotify integration
This commit is contained in:
1 parent
e36fb14a34
commit
c9cf29e5c0
32 files changed
+3176
-50
No files matched your search
@@ -11,6 +11,8 @@ import {
|
||||
type MediaPlaybackState,
|
||||
type MediaQueueItem,
|
||||
} from "./media-types";
|
||||
import { spotifyService } from "./spotify.ios";
|
||||
import type { SpotifyPlayer } from "./spotify-player";
|
||||
|
||||
export type { MediaControllerState, MediaPlaybackState, MediaQueueItem } from "./media-types";
|
||||
|
||||
@@ -88,7 +90,7 @@ export class AppleMusicControllerBridge implements MediaControllerBridge {
|
||||
onStateChange(listener: (state: MediaControllerState) => void): () => void {
|
||||
void this.start();
|
||||
this.stateListeners.add(listener);
|
||||
this.pollTimer ??= setInterval(() => this.refresh(), POLL_MS);
|
||||
this.pollTimer ??= setInterval(() => this.publish(), POLL_MS);
|
||||
listener(this.snapshot());
|
||||
return () => {
|
||||
this.stateListeners.delete(listener);
|
||||
@@ -142,7 +144,7 @@ export class AppleMusicControllerBridge implements MediaControllerBridge {
|
||||
if (this.hasAccess()) return Promise.resolve(true);
|
||||
this.pendingAccess ??= this.askForAccess().finally(() => {
|
||||
this.pendingAccess = null;
|
||||
this.refresh();
|
||||
this.publish();
|
||||
});
|
||||
return this.pendingAccess;
|
||||
}
|
||||
@@ -187,12 +189,12 @@ export class AppleMusicControllerBridge implements MediaControllerBridge {
|
||||
const music = FaceclawAppleMusic.shared();
|
||||
if (!this.bound) {
|
||||
this.bound = true;
|
||||
music.changeHandler = () => this.refresh();
|
||||
music.changeHandler = () => this.publish();
|
||||
}
|
||||
return music;
|
||||
}
|
||||
|
||||
private refresh(): void {
|
||||
private publish(): void {
|
||||
if (!this.stateListeners.size) return;
|
||||
const state = this.snapshot();
|
||||
const signature = stateSignature(state);
|
||||
@@ -228,4 +230,192 @@ export class AppleMusicControllerBridge implements MediaControllerBridge {
|
||||
}
|
||||
}
|
||||
|
||||
export const mediaControllerBridge: MediaControllerBridge = new AppleMusicControllerBridge();
|
||||
export type IosMediaSource = "apple-music" | "spotify";
|
||||
|
||||
/**
|
||||
* Which player to follow: the one playing; with both playing, the current
|
||||
* choice (no flapping); with neither, the one that played last, and before
|
||||
* either has played this run, Spotify when it reports a session (its player
|
||||
* reports only a recently active one, while the system Music player keeps
|
||||
* its last item for days).
|
||||
*/
|
||||
export function chooseMediaSource(
|
||||
current: IosMediaSource,
|
||||
appleMusic: MediaControllerState,
|
||||
spotify: MediaControllerState | null,
|
||||
appleMusicLastPlayingAtMs: number,
|
||||
spotifyLastPlayingAtMs: number,
|
||||
): IosMediaSource {
|
||||
if (!spotify?.accessEnabled) return "apple-music";
|
||||
if (!appleMusic.accessEnabled) return "spotify";
|
||||
const applePlaying = appleMusic.playbackState === "playing";
|
||||
const spotifyPlaying = spotify.playbackState === "playing";
|
||||
if (applePlaying && spotifyPlaying) return current;
|
||||
if (applePlaying) return "apple-music";
|
||||
if (spotifyPlaying) return "spotify";
|
||||
if (appleMusicLastPlayingAtMs !== spotifyLastPlayingAtMs) {
|
||||
return spotifyLastPlayingAtMs > appleMusicLastPlayingAtMs ? "spotify" : "apple-music";
|
||||
}
|
||||
return spotify.available ? "spotify" : "apple-music";
|
||||
}
|
||||
|
||||
/**
|
||||
* The iOS Music bridge: Apple Music (the system player), plus Spotify once
|
||||
* signed in (its Web API: playback on this phone or any Spotify Connect
|
||||
* device). The state and every command follow one of them, by
|
||||
* chooseMediaSource.
|
||||
*/
|
||||
export class IosMediaControllerBridge implements MediaControllerBridge {
|
||||
readonly accessPrompt =
|
||||
"Faceclaw needs access to Apple Music to show and control what's playing. Tap, then allow access on your phone. For Spotify, sign in from the app menu instead.";
|
||||
readonly accessLabel: string;
|
||||
readonly accessAction: string;
|
||||
private source: IosMediaSource = "apple-music";
|
||||
private appleMusicLastPlayingAtMs = 0;
|
||||
private readonly listeners = new Set<(state: MediaControllerState) => void>();
|
||||
private unsubscribeSources: Array<() => void> = [];
|
||||
/** The source and its artVersion the composite artVersion was bumped for. */
|
||||
private artKey = "";
|
||||
private artVersion = 0;
|
||||
|
||||
constructor(
|
||||
private readonly appleMusic: AppleMusicControllerBridge,
|
||||
private readonly spotify: () => SpotifyPlayer | null,
|
||||
private readonly now: () => number = Date.now,
|
||||
) {
|
||||
this.accessLabel = appleMusic.accessLabel;
|
||||
this.accessAction = appleMusic.accessAction;
|
||||
}
|
||||
|
||||
get skipRebuildsQueue(): boolean {
|
||||
return this.active().skipRebuildsQueue;
|
||||
}
|
||||
|
||||
onStateChange(listener: (state: MediaControllerState) => void): () => void {
|
||||
if (!this.listeners.size) {
|
||||
const update = () => this.emit();
|
||||
const spotify = this.spotify();
|
||||
this.unsubscribeSources = [this.appleMusic.onStateChange(update), ...(spotify ? [spotify.onStateChange(update)] : [])];
|
||||
}
|
||||
this.listeners.add(listener);
|
||||
listener(this.snapshot());
|
||||
return () => {
|
||||
this.listeners.delete(listener);
|
||||
if (this.listeners.size) return;
|
||||
for (const unsubscribe of this.unsubscribeSources) unsubscribe();
|
||||
this.unsubscribeSources = [];
|
||||
};
|
||||
}
|
||||
|
||||
snapshot(): MediaControllerState {
|
||||
const appleMusic = this.appleMusic.snapshot();
|
||||
const spotify = this.spotify()?.snapshot() ?? null;
|
||||
this.select(appleMusic, spotify);
|
||||
const state = this.source === "spotify" && spotify ? spotify : appleMusic;
|
||||
// Changes with the source as well as with that source's art.
|
||||
const artKey = `${this.source}:${state.artVersion}`;
|
||||
if (artKey !== this.artKey) {
|
||||
this.artKey = artKey;
|
||||
this.artVersion++;
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
status: state.available ? state.status : idleStatus(appleMusic, spotify, state),
|
||||
artVersion: this.artVersion,
|
||||
// A Spotify command can fail while Apple Music is the one shown (playing from the Spotify library).
|
||||
notice: spotify?.notice || state.notice || "",
|
||||
};
|
||||
}
|
||||
|
||||
async start(): Promise<void> {
|
||||
await this.appleMusic.start();
|
||||
}
|
||||
|
||||
async stop(): Promise<void> {
|
||||
await this.appleMusic.stop();
|
||||
}
|
||||
|
||||
playPause(): Promise<void> {
|
||||
return this.active().playPause();
|
||||
}
|
||||
|
||||
skipNext(): Promise<void> {
|
||||
return this.active().skipNext();
|
||||
}
|
||||
|
||||
skipPrevious(): Promise<void> {
|
||||
return this.active().skipPrevious();
|
||||
}
|
||||
|
||||
/** Apple Music's media-library permission (what requestAccess asks for); Spotify signs in from the Music menu. */
|
||||
hasAccess(): boolean {
|
||||
return this.appleMusic.hasAccess();
|
||||
}
|
||||
|
||||
requestAccess(): Promise<boolean> {
|
||||
return this.appleMusic.requestAccess();
|
||||
}
|
||||
|
||||
skipToQueueItem(id: number): Promise<void> {
|
||||
return this.active().skipToQueueItem(id);
|
||||
}
|
||||
|
||||
getMediaVolumePercent(): number {
|
||||
return this.active().getMediaVolumePercent();
|
||||
}
|
||||
|
||||
setMediaVolumePercent(volumePercent: number): void {
|
||||
this.active().setMediaVolumePercent(volumePercent);
|
||||
}
|
||||
|
||||
getAlbumArt(maxSize: number): GrayImage | null {
|
||||
return this.active().getAlbumArt(maxSize);
|
||||
}
|
||||
|
||||
getQueue(): MediaQueueItem[] {
|
||||
return this.active().getQueue();
|
||||
}
|
||||
|
||||
async sendCustomAction(action: string): Promise<void> {
|
||||
await this.active().sendCustomAction?.(action);
|
||||
}
|
||||
|
||||
async refresh(): Promise<void> {
|
||||
await this.spotify()?.refresh();
|
||||
}
|
||||
|
||||
private active(): MediaControllerBridge {
|
||||
const spotify = this.spotify();
|
||||
this.select(this.appleMusic.snapshot(), spotify?.snapshot() ?? null);
|
||||
return this.source === "spotify" && spotify ? spotify : this.appleMusic;
|
||||
}
|
||||
|
||||
private select(appleMusic: MediaControllerState, spotify: MediaControllerState | null): void {
|
||||
if (appleMusic.playbackState === "playing") this.appleMusicLastPlayingAtMs = this.now();
|
||||
this.source = chooseMediaSource(this.source, appleMusic, spotify, this.appleMusicLastPlayingAtMs,
|
||||
this.spotify()?.lastPlayingAtMs ?? 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Forward every source update: each source already drops repeats, and
|
||||
* Spotify also signals changes that are not in the state (its queue).
|
||||
*/
|
||||
private emit(): void {
|
||||
if (!this.listeners.size) return;
|
||||
const state = this.snapshot();
|
||||
for (const listener of this.listeners) listener(state);
|
||||
}
|
||||
}
|
||||
|
||||
/** The no-session line: which players were checked. */
|
||||
function idleStatus(appleMusic: MediaControllerState, spotify: MediaControllerState | null, shown: MediaControllerState): string {
|
||||
if (appleMusic.accessEnabled && spotify?.accessEnabled) return "Nothing is playing.";
|
||||
return shown.status;
|
||||
}
|
||||
|
||||
export const appleMusicControllerBridge = new AppleMusicControllerBridge();
|
||||
|
||||
export const mediaControllerBridge: MediaControllerBridge = new IosMediaControllerBridge(
|
||||
appleMusicControllerBridge,
|
||||
() => spotifyService()?.player ?? null,
|
||||
);
|
||||
Reference in new issue
Block a user