fix mpv player queue behavior to handle gapless playback

This commit is contained in:
jeffvli
2025-12-11 20:36:47 -08:00
parent f7d488ba84
commit 61e70342a4
7 changed files with 283 additions and 102 deletions
@@ -22,10 +22,13 @@ interface PlayerEvents {
interface PlayerEventsCallbacks {
onCurrentSongChange?: (
properties: { index: number; remaining: number; song: QueueSong | undefined },
prev: { index: number; remaining: number; song: QueueSong | undefined },
properties: { index: number; song: QueueSong | undefined },
prev: { index: number; song: QueueSong | undefined },
) => void;
onMediaNext?: (properties: { currentIndex: number; nextIndex: number }) => void;
onMediaPrev?: (properties: { currentIndex: number; prevIndex: number }) => void;
onPlayerMute?: (properties: { muted: boolean }, prev: { muted: boolean }) => void;
onPlayerPlay?: (properties: { id: string; index: number }) => void;
onPlayerProgress?: (properties: { timestamp: number }, prev: { timestamp: number }) => void;
onPlayerQueueChange?: (queue: QueueData, prev: QueueData) => void;
onPlayerRepeat?: (properties: { repeat: PlayerRepeat }, prev: { repeat: PlayerRepeat }) => void;
@@ -129,23 +132,44 @@ function createPlayerEvents(callbacks: PlayerEventsCallbacks): PlayerEvents {
unsubscribers.push(unsubscribe);
}
if (callbacks.onUserRating) {
eventEmitter.on('USER_RATING', callbacks.onUserRating);
if (callbacks.onMediaNext) {
eventEmitter.on('MEDIA_NEXT', callbacks.onMediaNext);
}
if (callbacks.onMediaPrev) {
eventEmitter.on('MEDIA_PREV', callbacks.onMediaPrev);
}
if (callbacks.onPlayerPlay) {
eventEmitter.on('PLAYER_PLAY', callbacks.onPlayerPlay);
}
if (callbacks.onUserFavorite) {
eventEmitter.on('USER_FAVORITE', callbacks.onUserFavorite);
}
if (callbacks.onUserRating) {
eventEmitter.on('USER_RATING', callbacks.onUserRating);
}
return {
cleanup: () => {
unsubscribers.forEach((unsubscribe) => unsubscribe());
if (callbacks.onUserRating) {
eventEmitter.off('USER_RATING', callbacks.onUserRating);
if (callbacks.onMediaNext) {
eventEmitter.off('MEDIA_NEXT', callbacks.onMediaNext);
}
if (callbacks.onMediaPrev) {
eventEmitter.off('MEDIA_PREV', callbacks.onMediaPrev);
}
if (callbacks.onPlayerPlay) {
eventEmitter.off('PLAYER_PLAY', callbacks.onPlayerPlay);
}
if (callbacks.onUserFavorite) {
eventEmitter.off('USER_FAVORITE', callbacks.onUserFavorite);
}
if (callbacks.onUserRating) {
eventEmitter.off('USER_RATING', callbacks.onUserRating);
}
},
};
}
@@ -47,3 +47,15 @@ export function useSongUrl(
transcode.enabled,
]);
}
export const getSongUrl = (song: QueueSong, transcode: TranscodingConfig) => {
return api.controller.getStreamUrl({
apiClientProps: { serverId: song._serverId },
query: {
bitrate: transcode.bitrate,
format: transcode.format,
id: song.id,
transcode: transcode.enabled,
},
});
};