Design Prot Merge #1
@@ -137,10 +137,12 @@ class PlayerState {
|
|||||||
audio.addEventListener('play', () => {
|
audio.addEventListener('play', () => {
|
||||||
this.playing = true;
|
this.playing = true;
|
||||||
this.#startProgressLoop();
|
this.#startProgressLoop();
|
||||||
|
this.#syncMediaSessionPlaybackState();
|
||||||
});
|
});
|
||||||
audio.addEventListener('pause', () => {
|
audio.addEventListener('pause', () => {
|
||||||
this.playing = false;
|
this.playing = false;
|
||||||
this.#stopProgressLoop();
|
this.#stopProgressLoop();
|
||||||
|
this.#syncMediaSessionPlaybackState();
|
||||||
});
|
});
|
||||||
audio.addEventListener('ended', () => {
|
audio.addEventListener('ended', () => {
|
||||||
this.#stopProgressLoop();
|
this.#stopProgressLoop();
|
||||||
@@ -162,9 +164,37 @@ class PlayerState {
|
|||||||
this.error = 'No se pudo reproducir la pista.';
|
this.error = 'No se pudo reproducir la pista.';
|
||||||
});
|
});
|
||||||
this.#audio = audio;
|
this.#audio = audio;
|
||||||
|
this.#setupMediaSession(audio);
|
||||||
return audio;
|
return audio;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#setupMediaSession(audio: HTMLAudioElement) {
|
||||||
|
if (!('mediaSession' in navigator)) return;
|
||||||
|
navigator.mediaSession.setActionHandler('play', () => audio.play().catch(() => {}));
|
||||||
|
navigator.mediaSession.setActionHandler('pause', () => audio.pause());
|
||||||
|
navigator.mediaSession.setActionHandler('previoustrack', () => this.previous());
|
||||||
|
navigator.mediaSession.setActionHandler('nexttrack', () => this.next());
|
||||||
|
}
|
||||||
|
|
||||||
|
#syncMediaSessionPlaybackState() {
|
||||||
|
if (!browser || !('mediaSession' in navigator)) return;
|
||||||
|
navigator.mediaSession.playbackState = this.playing ? 'playing' : 'paused';
|
||||||
|
}
|
||||||
|
|
||||||
|
#syncMediaSessionMetadata(track: PlayerTrack) {
|
||||||
|
if (!browser || !('mediaSession' in navigator)) return;
|
||||||
|
if (!track.id) {
|
||||||
|
navigator.mediaSession.metadata = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const artwork = track.coverUrl ?? `/api/tracks/${track.id}/cover?size=small`;
|
||||||
|
navigator.mediaSession.metadata = new MediaMetadata({
|
||||||
|
title: track.title,
|
||||||
|
artist: track.artist,
|
||||||
|
artwork: [{ src: artwork, sizes: '256x256' }]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
#tickProgress = () => {
|
#tickProgress = () => {
|
||||||
const audio = this.#audio;
|
const audio = this.#audio;
|
||||||
if (!audio) return;
|
if (!audio) return;
|
||||||
@@ -229,6 +259,7 @@ class PlayerState {
|
|||||||
const id = this.currentId;
|
const id = this.currentId;
|
||||||
const track = this.tracks.find((t) => t.id === id);
|
const track = this.tracks.find((t) => t.id === id);
|
||||||
this.#applyAccent(id, track?.coverUrl);
|
this.#applyAccent(id, track?.coverUrl);
|
||||||
|
if (track) this.#syncMediaSessionMetadata(track);
|
||||||
const token = ++this.#loadToken;
|
const token = ++this.#loadToken;
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
this.error = '';
|
this.error = '';
|
||||||
|
|||||||
Reference in New Issue
Block a user