From 6ca507933a00df660403995f53de4fb2ce1e59cb Mon Sep 17 00:00:00 2001 From: SimolZimol <70102430+SimolZimol@users.noreply.github.com> Date: Sat, 7 Jun 2025 13:33:56 +0200 Subject: [PATCH] modified: templates/quiz_multiplayer.html --- templates/quiz_multiplayer.html | 34 +++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/templates/quiz_multiplayer.html b/templates/quiz_multiplayer.html index eb18682..789ab0c 100644 --- a/templates/quiz_multiplayer.html +++ b/templates/quiz_multiplayer.html @@ -177,8 +177,13 @@ function quizifyReady() { // Musik abspielen, wenn Popup geschlossen - if (window.spotifyPlayer && window.spotifyPlayer._options && window.spotifyPlayer._options.getOAuthToken) { + function playTrackWhenReady() { const device_id = document.getElementById('device_id').value; + if (!device_id) { + // Player ist noch nicht bereit, nochmal versuchen + setTimeout(playTrackWhenReady, 200); + return; + } const playDuration = getPlayDuration(); const startPosition = getOption('startPosition', 'start'); let position_ms = 0; @@ -190,10 +195,31 @@ if (playDuration > 0) { window.quizifyTimeout = setTimeout(() => { window.spotifyPlayer.pause(); }, playDuration * 1000); } - } else if (window.onSpotifyWebPlaybackSDKReady) { - window.onSpotifyWebPlaybackSDKReady(); + setCorrectAnswer(); + } + + // Player initialisieren, falls nicht vorhanden + if (!window.spotifyPlayer) { + window.onSpotifyWebPlaybackSDKReady = () => { + const token = '{{ access_token }}'; + const player = new Spotify.Player({ + name: 'Musik Quiz Player', + getOAuthToken: cb => { cb(token); }, + volume: 0.5 + }); + + player.addListener('ready', ({ device_id }) => { + document.getElementById('device_id').value = device_id; + playTrackWhenReady(); + }); + + player.connect(); + window.spotifyPlayer = player; + }; + if (window.onSpotifyWebPlaybackSDKReady) window.onSpotifyWebPlaybackSDKReady(); + } else { + playTrackWhenReady(); } - setCorrectAnswer(); } function setCorrectAnswer() {