|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html> |
3 | 3 | <head> |
4 | | - <title>Spotify Dev Player</title> |
| 4 | + <title>Spotify Track Player</title> |
5 | 5 | <style> |
6 | 6 | body { font-family: Arial, sans-serif; max-width: 800px; margin: 2rem auto; padding: 0 1rem; } |
7 | 7 | .credentials { display: flex; flex-direction: column; gap: 1rem; margin-bottom: 2rem; } |
8 | 8 | input { padding: 0.5rem; width: 100%; } |
9 | | - button { padding: 0.5rem 1rem; cursor: pointer; margin: 0.5rem; } |
| 9 | + button { padding: 0.5rem 1rem; cursor: pointer; } |
10 | 10 | .hidden { display: none; } |
11 | 11 | #status { margin: 1rem 0; padding: 1rem; background: #f0f0f0; } |
12 | 12 | </style> |
|
18 | 18 | </div> |
19 | 19 | <div id="player" class="hidden"> |
20 | 20 | <div id="status">Status: Initializing...</div> |
21 | | - <div> |
22 | | - <button onclick="handlePlay()">Play</button> |
23 | | - <button onclick="handlePause()">Pause</button> |
24 | | - <button onclick="handlePrevious()">Previous</button> |
25 | | - <button onclick="handleNext()">Next</button> |
26 | | - <button onclick="handleSeek()">Seek to 0</button> |
27 | | - <button onclick="getCurrentState()">Get Current State</button> |
28 | | - <button onclick="getVolume()">Get Volume</button> |
29 | | - <button onclick="setVolume(0.5)">Set Volume 50%</button> |
30 | | - </div> |
31 | | - <div> |
32 | | - <input type="text" id="trackUri" placeholder="Spotify URI (e.g., spotify:track:xxx)"> |
33 | | - <button onclick="playTrack()">Play This Track</button> |
34 | | - </div> |
35 | 21 | </div> |
36 | 22 |
|
37 | 23 | <script> |
|
45 | 31 | } |
46 | 32 |
|
47 | 33 | function getUrlParameter(name) { |
48 | | - console.log('getUrlParameter', name) |
49 | 34 | const urlParams = new URLSearchParams(window.location.search); |
50 | 35 | return urlParams.get(name); |
51 | 36 | } |
52 | 37 |
|
53 | 38 | window.onSpotifyWebPlaybackSDKReady = () => { |
54 | 39 | const params = getHashParams(); |
55 | 40 | if (params.access_token) { |
| 41 | + localStorage.setItem('spotify_access_token', params.access_token); |
56 | 42 | initializePlayer(params.access_token); |
| 43 | + } else { |
| 44 | + const storedToken = localStorage.getItem('spotify_access_token'); |
| 45 | + if (storedToken) { |
| 46 | + initializePlayer(storedToken); |
| 47 | + } |
57 | 48 | } |
58 | 49 | }; |
59 | 50 |
|
60 | 51 | async function startAuth() { |
61 | 52 | const clientId = document.getElementById('clientId').value; |
| 53 | + localStorage.setItem('spotify_client_id', clientId); |
62 | 54 | const state = Math.random().toString(36).substring(7); |
63 | 55 | const scope = 'streaming user-read-email user-read-private user-modify-playback-state'; |
64 | 56 | const authUrl = `https://accounts.spotify.com/authorize?client_id=${clientId}&response_type=token&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${encodeURIComponent(scope)}&state=${state}`; |
|
89 | 81 |
|
90 | 82 | playerInstance.addListener('initialization_error', ({ message }) => { |
91 | 83 | updateStatus(`Initialization Error: ${message}`); |
| 84 | + localStorage.removeItem('spotify_access_token'); |
92 | 85 | }); |
93 | 86 |
|
94 | 87 | playerInstance.addListener('authentication_error', ({ message }) => { |
95 | 88 | updateStatus(`Authentication Error: ${message}`); |
| 89 | + localStorage.removeItem('spotify_access_token'); |
96 | 90 | document.getElementById('setup').classList.remove('hidden'); |
97 | 91 | document.getElementById('player').classList.add('hidden'); |
98 | 92 | }); |
99 | 93 |
|
100 | | - playerInstance.addListener('account_error', ({ message }) => { |
101 | | - updateStatus(`Account Error: ${message}`); |
102 | | - }); |
103 | | - |
104 | | - playerInstance.addListener('playback_error', ({ message }) => { |
105 | | - updateStatus(`Playback Error: ${message}`); |
106 | | - }); |
107 | | - |
108 | 94 | playerInstance.addListener('player_state_changed', state => { |
109 | 95 | if (state) { |
110 | | - updateStatus(`Track: ${state.track_window.current_track.name} | Artist: ${state.track_window.current_track.artists[0].name} | Playing: ${!state.paused}`); |
| 96 | + updateStatus(`Now Playing: ${state.track_window.current_track.name} by ${state.track_window.current_track.artists[0].name}`); |
111 | 97 | } |
112 | 98 | }); |
113 | 99 |
|
114 | 100 | playerInstance.addListener('ready', ({ device_id }) => { |
115 | | - updateStatus(`Ready with Device ID: ${device_id}`); |
| 101 | + updateStatus('Connecting to Spotify...'); |
116 | 102 | fetch('https://api.spotify.com/v1/me/player', { |
117 | 103 | method: 'PUT', |
118 | 104 | headers: { |
|
125 | 111 | }) |
126 | 112 | }).then(response => { |
127 | 113 | if (response.status === 204) { |
128 | | - updateStatus('Successfully transferred playback'); |
129 | 114 | const trackId = getUrlParameter('track'); |
130 | 115 | if (trackId) { |
131 | 116 | playTrackById(trackId, token); |
|
134 | 119 | }); |
135 | 120 | }); |
136 | 121 |
|
137 | | - playerInstance.addListener('not_ready', ({ device_id }) => { |
138 | | - updateStatus(`Device ID has gone offline: ${device_id}`); |
139 | | - }); |
140 | | - |
141 | 122 | playerInstance.connect(); |
142 | 123 | } |
143 | 124 |
|
|
154 | 135 | }) |
155 | 136 | }); |
156 | 137 | if (response.status === 204) { |
157 | | - updateStatus('Started playing track'); |
158 | | - } else { |
159 | | - updateStatus('Failed to play track: ' + response.status); |
| 138 | + updateStatus('Loading track...'); |
160 | 139 | } |
161 | 140 | } catch (error) { |
162 | 141 | updateStatus('Error playing track: ' + error); |
163 | 142 | } |
164 | 143 | } |
165 | 144 |
|
166 | | - async function handlePlay() { |
167 | | - const result = await playerInstance.resume(); |
168 | | - updateStatus(`Play result: ${JSON.stringify(result)}`); |
169 | | - } |
170 | | - |
171 | | - async function handlePause() { |
172 | | - const result = await playerInstance.pause(); |
173 | | - updateStatus(`Pause result: ${JSON.stringify(result)}`); |
174 | | - } |
175 | | - |
176 | | - async function handlePrevious() { |
177 | | - const result = await playerInstance.previousTrack(); |
178 | | - updateStatus(`Previous track result: ${JSON.stringify(result)}`); |
179 | | - } |
180 | | - |
181 | | - async function handleNext() { |
182 | | - const result = await playerInstance.nextTrack(); |
183 | | - updateStatus(`Next track result: ${JSON.stringify(result)}`); |
184 | | - } |
185 | | - |
186 | | - async function handleSeek() { |
187 | | - const result = await playerInstance.seek(0); |
188 | | - updateStatus(`Seek result: ${JSON.stringify(result)}`); |
189 | | - } |
190 | | - |
191 | | - async function getCurrentState() { |
192 | | - const state = await playerInstance.getCurrentState(); |
193 | | - if (state) { |
194 | | - updateStatus(`Current track: ${state.track_window.current_track.name} | Artist: ${state.track_window.current_track.artists[0].name} | Position: ${state.position} | Paused: ${state.paused}`); |
195 | | - } else { |
196 | | - updateStatus('No current state available'); |
197 | | - } |
198 | | - } |
199 | | - |
200 | | - async function getVolume() { |
201 | | - const volume = await playerInstance.getVolume(); |
202 | | - updateStatus(`Current volume: ${volume * 100}%`); |
203 | | - } |
204 | | - |
205 | | - async function setVolume(value) { |
206 | | - const result = await playerInstance.setVolume(value); |
207 | | - updateStatus(`Set volume result: ${JSON.stringify(result)}`); |
208 | | - } |
209 | | - |
210 | | - async function playTrack() { |
211 | | - const params = getHashParams(); |
212 | | - const token = params.access_token; |
213 | | - const trackUri = document.getElementById('trackUri').value; |
214 | | - try { |
215 | | - const response = await fetch('https://api.spotify.com/v1/me/player/play', { |
216 | | - method: 'PUT', |
217 | | - headers: { |
218 | | - 'Authorization': `Bearer ${token}`, |
219 | | - 'Content-Type': 'application/json' |
220 | | - }, |
221 | | - body: JSON.stringify({ |
222 | | - uris: [trackUri] |
223 | | - }) |
224 | | - }); |
225 | | - if (response.status === 204) { |
226 | | - updateStatus('Started playing track'); |
227 | | - } else { |
228 | | - updateStatus('Failed to play track: ' + response.status); |
229 | | - } |
230 | | - } catch (error) { |
231 | | - updateStatus('Error playing track: ' + error); |
232 | | - } |
| 145 | + const storedClientId = localStorage.getItem('spotify_client_id'); |
| 146 | + if (storedClientId) { |
| 147 | + document.getElementById('clientId').value = storedClientId; |
233 | 148 | } |
234 | 149 | </script> |
235 | 150 | <script src="https://sdk.scdn.co/spotify-player.js"></script> |
|
0 commit comments