|
140 | 140 | } |
141 | 141 | }); |
142 | 142 |
|
143 | | - playerInstance.addListener('ready', ({ device_id }) => { |
144 | | - fetch('https://api.spotify.com/v1/me/player', { |
145 | | - method: 'PUT', |
146 | | - headers: { |
147 | | - 'Authorization': `Bearer ${token}`, |
148 | | - 'Content-Type': 'application/json' |
149 | | - }, |
150 | | - body: JSON.stringify({ |
151 | | - device_ids: [device_id], |
152 | | - play: false |
153 | | - }) |
154 | | - }).then(response => { |
155 | | - if (response.status === 204) { |
156 | | - // Add a small delay to ensure the device is fully registered |
157 | | - setTimeout(() => { |
158 | | - const trackId = getUrlParameter('track'); |
159 | | - if (trackId) { |
160 | | - playTrackById(trackId, token); |
161 | | - } |
162 | | - }, 1000); |
| 143 | + let deviceReady = false; |
| 144 | + playerInstance.addListener('ready', async ({ device_id }) => { |
| 145 | + deviceReady = true; |
| 146 | + try { |
| 147 | + await fetch('https://api.spotify.com/v1/me/player', { |
| 148 | + method: 'PUT', |
| 149 | + headers: { |
| 150 | + 'Authorization': `Bearer ${token}`, |
| 151 | + 'Content-Type': 'application/json' |
| 152 | + }, |
| 153 | + body: JSON.stringify({ |
| 154 | + device_ids: [device_id], |
| 155 | + play: false |
| 156 | + }) |
| 157 | + }); |
| 158 | + |
| 159 | + const trackId = getUrlParameter('track'); |
| 160 | + if (trackId) { |
| 161 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 162 | + await fetch('https://api.spotify.com/v1/me/player/play', { |
| 163 | + method: 'PUT', |
| 164 | + headers: { |
| 165 | + 'Authorization': `Bearer ${token}`, |
| 166 | + 'Content-Type': 'application/json' |
| 167 | + }, |
| 168 | + body: JSON.stringify({ |
| 169 | + uris: [`spotify:track:${trackId}`] |
| 170 | + }) |
| 171 | + }); |
| 172 | + updatePlayPauseButton(true); |
163 | 173 | } |
164 | | - }); |
| 174 | + } catch (error) { |
| 175 | + console.error('Error during player initialization:', error); |
| 176 | + } |
165 | 177 | }); |
166 | 178 |
|
167 | 179 | playPauseButton.onclick = togglePlayPause; |
168 | 180 | playerInstance.connect(); |
169 | 181 | } |
170 | 182 |
|
171 | | - async function playTrackById(trackId, token) { |
172 | | - try { |
173 | | - const response = await fetch('https://api.spotify.com/v1/me/player/play', { |
174 | | - method: 'PUT', |
175 | | - headers: { |
176 | | - 'Authorization': `Bearer ${token}`, |
177 | | - 'Content-Type': 'application/json' |
178 | | - }, |
179 | | - body: JSON.stringify({ |
180 | | - uris: [`spotify:track:${trackId}`] |
181 | | - }) |
182 | | - }); |
183 | | - if (response.status === 204) { |
184 | | - updatePlayPauseButton(true); |
185 | | - } |
186 | | - } catch (error) { |
187 | | - console.error('Error playing track:', error); |
188 | | - } |
189 | | - } |
190 | | - |
191 | 183 | const storedClientId = localStorage.getItem('spotify_client_id'); |
192 | 184 | if (storedClientId) { |
193 | 185 | document.getElementById('clientId').value = storedClientId; |
|
0 commit comments