From 564f2d47b42f7a1370182c5a80d188301348079c Mon Sep 17 00:00:00 2001 From: Mark Hoekveen Date: Sun, 17 Aug 2025 18:03:50 +0200 Subject: [PATCH 1/3] spotify player --- frontend/src/app/lobby/page.tsx | 27 +++++++++++++++++++ frontend/src/app/style.css | 5 ++++ frontend/src/contexts/SpotifyIframe.tsx | 35 +++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 frontend/src/contexts/SpotifyIframe.tsx diff --git a/frontend/src/app/lobby/page.tsx b/frontend/src/app/lobby/page.tsx index 7189fbe..90af87a 100644 --- a/frontend/src/app/lobby/page.tsx +++ b/frontend/src/app/lobby/page.tsx @@ -7,6 +7,7 @@ import { useAuth } from "react-oidc-context"; import { useSession } from "muzak/contexts/SessionContext"; import { getUser, api, Track } from "muzak/data/fetcher"; import getSessionId from "muzak/data/session"; +import { useSpotifyIframe } from "muzak/contexts/SpotifyIframe"; const nunito = Nunito({ weight: "900", @@ -24,8 +25,10 @@ export default function Lobby() { const [sessionState, setSessionState] = useState("lobby"); const [currentSong, setCurrentSong] = useState(); const [songLoading, setSongLoading] = useState(false); + const [spotifyId, setSpotifyId] = useState(null); const auth = useAuth(); const user = getUser(); + const controller = useSpotifyIframe(); // Main message handler and dispatcher function onMessage(e) { @@ -165,6 +168,7 @@ export default function Lobby() { } else { nextTrack(); } + setSpotifyId(session?.current_track); setSongLoading(false); } @@ -178,9 +182,26 @@ export default function Lobby() { ); } + // Spotify Player stuff + useEffect(() => { + if (controller) { + console.log("Loaded track:", spotifyId); + controller.loadUri(`spotify:track:${spotifyId}`); + controller.play(); + } else { + console.log("Spotify player not initialized"); + } + }, [controller, spotifyId]); + async function playTrack() { + controller.play(); + } + if (sessionState == "lobby") { return (
+
+
+

+ { + const [controller, setController] = useState(null); + const elementId = "splayer"; + useEffect(() => { + const script = document.createElement("script"); + const spotifyScriptUrl = "https://open.spotify.com/embed/iframe-api/v1"; + const spotifyScriptElementId = "spotify-iframe-api"; + script.id = spotifyScriptElementId; + script.setAttribute("id", spotifyScriptElementId); + script.src = spotifyScriptUrl; + document.body.appendChild(script); + + script.onload = () => { + window.onSpotifyIframeApiReady = (IFrameAPI) => { + const element = document.getElementById(elementId); + const options = { + width: 400, + height: 400, + }; + const callback = (EmbedController) => { + setController(EmbedController); + }; + IFrameAPI.createController(element, options, callback); + }; + }; + + return () => { + document.body.removeChild(script); + }; + }, []); + + return controller; +}; From cfb6b2609b47a12cd5bafd196f45ae54c5351e06 Mon Sep 17 00:00:00 2001 From: Mark Hoekveen Date: Sun, 17 Aug 2025 20:25:47 +0200 Subject: [PATCH 2/3] ignores logs --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 79e9c33..c3fd7ae 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ wallpapers/ frontend/node_modules/ frontend/.next/ frontend/src/data/types.ts +backend/session_*.log From bd71bce450fcf120bbd5abbfc8c649174df0796b Mon Sep 17 00:00:00 2001 From: Mark Hoekveen Date: Sun, 17 Aug 2025 20:28:19 +0200 Subject: [PATCH 3/3] fixes compilation --- frontend/src/contexts/SpotifyIframe.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/contexts/SpotifyIframe.tsx b/frontend/src/contexts/SpotifyIframe.tsx index 25fae8e..3b3b83a 100644 --- a/frontend/src/contexts/SpotifyIframe.tsx +++ b/frontend/src/contexts/SpotifyIframe.tsx @@ -13,7 +13,7 @@ export const useSpotifyIframe = () => { document.body.appendChild(script); script.onload = () => { - window.onSpotifyIframeApiReady = (IFrameAPI) => { + (window as any).onSpotifyIframeApiReady = (IFrameAPI) => { const element = document.getElementById(elementId); const options = { width: 400,