Skip to content

Commit 77a93ac

Browse files
committed
- playing around with the spotify API
1 parent 1593435 commit 77a93ac

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

tools/spotify_qr.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ <h1 class="text-2xl font-bold text-center mb-6 text-gray-800">Spotify Track QR G
3030
Generate QR Code
3131
</button>
3232

33-
<div id="qrOutput" class="flex justify-center">
34-
<canvas id="qrcode" class="hidden"></canvas>
35-
</div>
33+
<div id="qrcode" class="flex justify-center"></div>
3634

3735
<div id="downloadButton" class="text-center hidden">
3836
<a
@@ -70,7 +68,6 @@ <h1 class="text-2xl font-bold text-center mb-6 text-gray-800">Spotify Track QR G
7068
correctLevel: QRCode.CorrectLevel.H
7169
});
7270

73-
qrcode.classList.remove('hidden');
7471
downloadButton.classList.remove('hidden');
7572

7673
setTimeout(() => {

tools/spotify_test.html

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<button class="setup-button" onclick="startAuth()">Initialize Player</button>
5555
</div>
5656
<div id="player" class="hidden">
57-
<button id="playPauseButton">▐▐</button>
57+
<button id="playPauseButton"></button>
5858
</div>
5959

6060
<script>
@@ -67,7 +67,12 @@
6767
if (isPlaying) {
6868
playerInstance.pause();
6969
} else {
70-
playerInstance.resume();
70+
const trackId = getUrlParameter('track');
71+
if (trackId) {
72+
playTrackById(trackId);
73+
} else {
74+
playerInstance.resume();
75+
}
7176
}
7277
}
7378

@@ -114,6 +119,27 @@
114119
return hashParams;
115120
}
116121

122+
async function playTrackById(trackId) {
123+
const token = localStorage.getItem('spotify_access_token');
124+
try {
125+
const response = await fetch('https://api.spotify.com/v1/me/player/play', {
126+
method: 'PUT',
127+
headers: {
128+
'Authorization': `Bearer ${token}`,
129+
'Content-Type': 'application/json'
130+
},
131+
body: JSON.stringify({
132+
uris: [`spotify:track:${trackId}`]
133+
})
134+
});
135+
if (response.status === 204) {
136+
updatePlayPauseButton(true);
137+
}
138+
} catch (error) {
139+
console.error('Error playing track:', error);
140+
}
141+
}
142+
117143
function initializePlayer(token) {
118144
document.getElementById('setup').classList.add('hidden');
119145
document.getElementById('player').classList.remove('hidden');
@@ -140,9 +166,7 @@
140166
}
141167
});
142168

143-
let deviceReady = false;
144169
playerInstance.addListener('ready', async ({ device_id }) => {
145-
deviceReady = true;
146170
try {
147171
await fetch('https://api.spotify.com/v1/me/player', {
148172
method: 'PUT',
@@ -155,22 +179,6 @@
155179
play: false
156180
})
157181
});
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);
173-
}
174182
} catch (error) {
175183
console.error('Error during player initialization:', error);
176184
}

0 commit comments

Comments
 (0)