|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html> |
3 | 3 | <head> |
4 | | - <title>Spotify Track Player</title> |
| 4 | + <title>Spotify Player</title> |
5 | 5 | <style> |
6 | | - body { |
7 | | - font-family: Arial, sans-serif; |
8 | | - margin: 0; |
9 | | - padding: 0; |
10 | | - background-color: #121212; |
11 | | - color: white; |
12 | | - height: 100vh; |
13 | | - display: flex; |
14 | | - flex-direction: column; |
15 | | - align-items: center; |
16 | | - } |
17 | | - |
18 | | - .credentials { |
19 | | - width: 80%; |
20 | | - max-width: 500px; |
21 | | - margin: 2rem auto; |
22 | | - display: flex; |
23 | | - flex-direction: column; |
24 | | - gap: 1rem; |
25 | | - } |
26 | | - |
27 | | - input { |
28 | | - padding: 1rem; |
29 | | - width: 100%; |
30 | | - background: #282828; |
31 | | - border: 1px solid #404040; |
32 | | - color: white; |
33 | | - border-radius: 8px; |
34 | | - font-size: 16px; |
35 | | - } |
36 | | - |
37 | | - .setup-button { |
38 | | - padding: 1rem; |
39 | | - cursor: pointer; |
40 | | - background: #1DB954; |
41 | | - border: none; |
42 | | - color: white; |
43 | | - border-radius: 8px; |
44 | | - font-size: 16px; |
45 | | - font-weight: bold; |
46 | | - } |
47 | | - |
48 | | - .hidden { |
49 | | - display: none; |
50 | | - } |
51 | | - |
52 | | - #player { |
53 | | - display: flex; |
54 | | - justify-content: center; |
55 | | - align-items: center; |
56 | | - flex-grow: 1; |
57 | | - } |
58 | | - |
59 | | - #pauseResumeButton { |
60 | | - width: 200px; |
61 | | - height: 200px; |
62 | | - border-radius: 50%; |
63 | | - border: none; |
64 | | - background: #1DB954; |
65 | | - color: white; |
66 | | - font-size: 60px; |
67 | | - cursor: pointer; |
68 | | - transition: transform 0.2s; |
69 | | - box-shadow: 0 8px 16px rgba(0,0,0,0.3); |
70 | | - } |
71 | | - |
72 | | - #pauseResumeButton:hover { |
73 | | - transform: scale(1.05); |
74 | | - } |
75 | | - |
76 | | - #pauseResumeButton:active { |
77 | | - transform: scale(0.95); |
78 | | - } |
| 6 | + .hidden { display: none; } |
79 | 7 | </style> |
80 | 8 | </head> |
81 | 9 | <body> |
82 | | -<div id="setup" class="credentials"> |
| 10 | +<div id="setup"> |
83 | 11 | <input type="text" id="clientId" placeholder="Enter Spotify Client ID"> |
84 | | - <button class="setup-button" onclick="startAuth()">Initialize Player</button> |
| 12 | + <button onclick="startAuth()">Initialize</button> |
85 | 13 | </div> |
86 | 14 | <div id="player" class="hidden"> |
87 | 15 | <button id="pauseResumeButton">⏸️</button> |
|
91 | 19 | let playerInstance = null; |
92 | 20 | let isPlaying = false; |
93 | 21 | const redirectUri = 'http://gptgames.dev/tools/spotify_test.html'; |
94 | | - const pauseResumeButton = document.getElementById('pauseResumeButton'); |
95 | 22 |
|
96 | 23 | function togglePauseResume() { |
97 | 24 | if (isPlaying) { |
98 | 25 | playerInstance.pause(); |
99 | 26 | } else { |
100 | 27 | playerInstance.resume(); |
101 | 28 | } |
102 | | - } |
103 | | - |
104 | | - function updatePauseResumeButton(playing) { |
105 | | - isPlaying = playing; |
| 29 | + isPlaying = !isPlaying; |
106 | 30 | pauseResumeButton.textContent = isPlaying ? '⏸️' : '▶️'; |
107 | 31 | } |
108 | 32 |
|
109 | 33 | function getUrlParameter(name) { |
110 | | - const urlParams = new URLSearchParams(window.location.search); |
111 | | - return urlParams.get(name); |
| 34 | + return new URLSearchParams(window.location.search).get(name); |
112 | 35 | } |
113 | 36 |
|
114 | 37 | window.onSpotifyWebPlaybackSDKReady = () => { |
115 | | - const params = getHashParams(); |
116 | | - if (params.access_token) { |
117 | | - localStorage.setItem('spotify_access_token', params.access_token); |
118 | | - initializePlayer(params.access_token); |
119 | | - } else { |
120 | | - const storedToken = localStorage.getItem('spotify_access_token'); |
121 | | - if (storedToken) { |
122 | | - initializePlayer(storedToken); |
123 | | - } |
124 | | - } |
| 38 | + const token = localStorage.getItem('spotify_access_token') || getHashParams().access_token; |
| 39 | + if (token) initializePlayer(token); |
125 | 40 | }; |
126 | 41 |
|
127 | | - async function startAuth() { |
| 42 | + function startAuth() { |
128 | 43 | const clientId = document.getElementById('clientId').value; |
129 | 44 | localStorage.setItem('spotify_client_id', clientId); |
130 | | - const state = Math.random().toString(36).substring(7); |
131 | 45 | const scope = 'streaming user-read-email user-read-private user-modify-playback-state'; |
132 | | - const authUrl = `https://accounts.spotify.com/authorize?client_id=${clientId}&response_type=token&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${encodeURIComponent(scope)}&state=${state}`; |
133 | | - window.location.href = authUrl; |
| 46 | + window.location.href = `https://accounts.spotify.com/authorize?client_id=${clientId}&response_type=token&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${encodeURIComponent(scope)}`; |
134 | 47 | } |
135 | 48 |
|
136 | 49 | function getHashParams() { |
137 | 50 | const hashParams = {}; |
138 | | - const hash = window.location.hash.substring(1); |
139 | | - const params = hash.split('&'); |
140 | | - params.forEach(param => { |
| 51 | + window.location.hash.substring(1).split('&').forEach(param => { |
141 | 52 | const [key, value] = param.split('='); |
142 | 53 | hashParams[key] = decodeURIComponent(value); |
143 | 54 | }); |
|
150 | 61 |
|
151 | 62 | playerInstance = new Spotify.Player({ |
152 | 63 | name: 'Development Player', |
153 | | - getOAuthToken: cb => { cb(token); }, |
154 | | - volume: 1.0 |
155 | | - }); |
156 | | - |
157 | | - playerInstance.addListener('initialization_error', () => { |
158 | | - localStorage.removeItem('spotify_access_token'); |
159 | | - }); |
160 | | - |
161 | | - playerInstance.addListener('authentication_error', () => { |
162 | | - localStorage.removeItem('spotify_access_token'); |
163 | | - document.getElementById('setup').classList.remove('hidden'); |
164 | | - document.getElementById('player').classList.add('hidden'); |
165 | | - }); |
166 | | - |
167 | | - playerInstance.addListener('player_state_changed', state => { |
168 | | - if (state) { |
169 | | - updatePauseResumeButton(!state.paused); |
170 | | - } |
| 64 | + getOAuthToken: cb => { cb(token); } |
171 | 65 | }); |
172 | 66 |
|
173 | 67 | playerInstance.addListener('ready', async ({ device_id }) => { |
174 | | - try { |
175 | | - // First, become active device |
176 | | - await fetch('https://api.spotify.com/v1/me/player', { |
| 68 | + await fetch('https://api.spotify.com/v1/me/player', { |
| 69 | + method: 'PUT', |
| 70 | + headers: { |
| 71 | + 'Authorization': `Bearer ${token}`, |
| 72 | + 'Content-Type': 'application/json' |
| 73 | + }, |
| 74 | + body: JSON.stringify({ device_ids: [device_id], play: true }) |
| 75 | + }); |
| 76 | + |
| 77 | + const trackId = getUrlParameter('track'); |
| 78 | + if (trackId) { |
| 79 | + await fetch(`https://api.spotify.com/v1/me/player/play?device_id=${device_id}`, { |
177 | 80 | method: 'PUT', |
178 | 81 | headers: { |
179 | 82 | 'Authorization': `Bearer ${token}`, |
180 | 83 | 'Content-Type': 'application/json' |
181 | 84 | }, |
182 | | - body: JSON.stringify({ |
183 | | - device_ids: [device_id], |
184 | | - play: true // Changed to true to ensure playback starts |
185 | | - }) |
| 85 | + body: JSON.stringify({ uris: [`spotify:track:${trackId}`] }) |
186 | 86 | }); |
187 | | - |
188 | | - // Then, after a short delay, play the track if specified |
189 | | - const trackId = getUrlParameter('track'); |
190 | | - if (trackId) { |
191 | | - // Add a small delay to ensure device is ready |
192 | | - setTimeout(async () => { |
193 | | - try { |
194 | | - await fetch(`https://api.spotify.com/v1/me/player/play?device_id=${device_id}`, { |
195 | | - method: 'PUT', |
196 | | - headers: { |
197 | | - 'Authorization': `Bearer ${token}`, |
198 | | - 'Content-Type': 'application/json' |
199 | | - }, |
200 | | - body: JSON.stringify({ |
201 | | - uris: [`spotify:track:${trackId}`] |
202 | | - }) |
203 | | - }); |
204 | | - } catch (error) { |
205 | | - console.error('Error playing track:', error); |
206 | | - } |
207 | | - }, 1000); |
208 | | - } |
209 | | - } catch (error) { |
210 | | - console.error('Error during player initialization:', error); |
211 | 87 | } |
212 | 88 | }); |
213 | 89 |
|
214 | | - pauseResumeButton.onclick = togglePauseResume; |
| 90 | + document.getElementById('pauseResumeButton').onclick = togglePauseResume; |
215 | 91 | playerInstance.connect(); |
216 | 92 | } |
217 | | - |
218 | | - const storedClientId = localStorage.getItem('spotify_client_id'); |
219 | | - if (storedClientId) { |
220 | | - document.getElementById('clientId').value = storedClientId; |
221 | | - } |
222 | 93 | </script> |
223 | 94 | <script src="https://sdk.scdn.co/spotify-player.js"></script> |
224 | 95 | </body> |
|
0 commit comments