Skip to content

Commit 2d126c5

Browse files
committed
- playing around with the spotify API
1 parent edc0f5a commit 2d126c5

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tools/spotify_test.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
<button onclick="getVolume()">Get Volume</button>
5353
<button onclick="setVolume(0.5)">Set Volume 50%</button>
5454
</div>
55+
<div>
56+
<input type="text" id="trackUri" placeholder="Spotify URI (e.g., spotify:track:xxx)">
57+
<button onclick="playTrack()">Play This Track</button>
58+
</div>
5559
</div>
5660

5761
<script>
@@ -203,6 +207,33 @@
203207
const result = await playerInstance.setVolume(value);
204208
updateStatus(`Set volume result: ${JSON.stringify(result)}`);
205209
}
210+
211+
async function playTrack() {
212+
const params = getHashParams();
213+
const token = params.access_token;
214+
const trackUri = document.getElementById('trackUri').value;
215+
216+
try {
217+
const response = await fetch('https://api.spotify.com/v1/me/player/play', {
218+
method: 'PUT',
219+
headers: {
220+
'Authorization': `Bearer ${token}`,
221+
'Content-Type': 'application/json'
222+
},
223+
body: JSON.stringify({
224+
uris: [trackUri]
225+
})
226+
});
227+
228+
if (response.status === 204) {
229+
updateStatus('Started playing track');
230+
} else {
231+
updateStatus('Failed to play track: ' + response.status);
232+
}
233+
} catch (error) {
234+
updateStatus('Error playing track: ' + error);
235+
}
236+
}
206237
</script>
207238
<script src="https://sdk.scdn.co/spotify-player.js"></script>
208239
</body>

0 commit comments

Comments
 (0)