diff --git a/src/utils/methods/transferPlayback.ts b/src/utils/methods/transferPlayback.ts index bf0bc12..5aad17a 100644 --- a/src/utils/methods/transferPlayback.ts +++ b/src/utils/methods/transferPlayback.ts @@ -30,17 +30,33 @@ export const transferSessionToLocal = async ( userId: string | undefined, session: SessionInfoDto, ): Promise<"audio" | "video" | "none"> => { - const item = session.NowPlayingItem; - if (!api || !userId || !item?.Id || !session.Id) { + if (!api || !userId || !session.Id) { return "none"; } - const positionTicks = session.PlayState?.PositionTicks ?? 0; + // The bar/dialog polls sessions every few seconds; refresh the target so a + // seek made in the meantime is reflected in the transfer position. + let currentSession = session; + try { + const fresh = (await getSessionApi(api).getSessions()).data.find( + (entry) => entry.Id === session.Id, + ); + if (fresh) currentSession = fresh; + } catch (error) { + console.warn("Could not refresh the remote session", error); + } + + const item = currentSession.NowPlayingItem; + if (!item?.Id) { + return "none"; + } + + const positionTicks = currentSession.PlayState?.PositionTicks ?? 0; // Rebuild the remote queue when the server reports it. let queue: BaseItemDto[] = [item]; let queueIndex = 0; - const queueIds = (session.NowPlayingQueue ?? []) + const queueIds = (currentSession.NowPlayingQueue ?? []) .map((queueItem) => queueItem.Id) .filter((id): id is string => Boolean(id)); if (queueIds.length > 0) { @@ -107,7 +123,7 @@ export const transferSessionToLocal = async ( // Transfer, not cast: the remote device should stop. try { await getSessionApi(api).sendPlaystateCommand({ - sessionId: session.Id, + sessionId: currentSession.Id, command: "Stop", controllingUserId: userId, });