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() {