Skip to content

Commit c9fcc4f

Browse files
committed
- playing around with the spotify API
1 parent 80e53ff commit c9fcc4f

File tree

1 file changed

+51
-39
lines changed

1 file changed

+51
-39
lines changed

tools/spotify_test.html

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,74 @@
55
<style>
66
body {
77
font-family: Arial, sans-serif;
8-
max-width: 800px;
9-
margin: 2rem auto;
10-
padding: 0 1rem;
8+
margin: 0;
9+
padding: 0;
1110
background-color: #121212;
1211
color: white;
12+
height: 100vh;
13+
display: flex;
14+
flex-direction: column;
15+
align-items: center;
1316
}
17+
1418
.credentials {
19+
width: 80%;
20+
max-width: 500px;
21+
margin: 2rem auto;
1522
display: flex;
1623
flex-direction: column;
1724
gap: 1rem;
18-
margin-bottom: 2rem;
1925
}
26+
2027
input {
21-
padding: 0.5rem;
28+
padding: 1rem;
2229
width: 100%;
2330
background: #282828;
2431
border: 1px solid #404040;
2532
color: white;
26-
border-radius: 4px;
33+
border-radius: 8px;
34+
font-size: 16px;
2735
}
36+
2837
.setup-button {
29-
padding: 0.75rem 1.5rem;
38+
padding: 1rem;
3039
cursor: pointer;
3140
background: #1DB954;
3241
border: none;
3342
color: white;
34-
border-radius: 4px;
43+
border-radius: 8px;
44+
font-size: 16px;
3545
font-weight: bold;
36-
transition: background-color 0.2s;
37-
}
38-
.setup-button:hover {
39-
background: #1ed760;
4046
}
47+
4148
.hidden {
4249
display: none;
4350
}
51+
4452
#player {
4553
display: flex;
4654
justify-content: center;
4755
align-items: center;
48-
min-height: 80vh;
56+
flex-grow: 1;
4957
}
58+
5059
#pauseResumeButton {
51-
width: 120px;
52-
height: 120px;
60+
width: 200px;
61+
height: 200px;
5362
border-radius: 50%;
5463
border: none;
55-
background: linear-gradient(145deg, #1DB954, #1ed760);
64+
background: #1DB954;
5665
color: white;
57-
font-size: 28px;
66+
font-size: 60px;
5867
cursor: pointer;
59-
transition: all 0.3s ease;
60-
box-shadow: 0 6px 12px rgba(29, 185, 84, 0.3);
61-
display: flex;
62-
align-items: center;
63-
justify-content: center;
68+
transition: transform 0.2s;
69+
box-shadow: 0 8px 16px rgba(0,0,0,0.3);
6470
}
71+
6572
#pauseResumeButton:hover {
6673
transform: scale(1.05);
67-
box-shadow: 0 8px 16px rgba(29, 185, 84, 0.4);
6874
}
75+
6976
#pauseResumeButton:active {
7077
transform: scale(0.95);
7178
}
@@ -144,7 +151,7 @@
144151
playerInstance = new Spotify.Player({
145152
name: 'Development Player',
146153
getOAuthToken: cb => { cb(token); },
147-
volume: 0.5
154+
volume: 1.0
148155
});
149156

150157
playerInstance.addListener('initialization_error', () => {
@@ -165,6 +172,7 @@
165172

166173
playerInstance.addListener('ready', async ({ device_id }) => {
167174
try {
175+
// First, become active device
168176
await fetch('https://api.spotify.com/v1/me/player', {
169177
method: 'PUT',
170178
headers: {
@@ -173,26 +181,30 @@
173181
},
174182
body: JSON.stringify({
175183
device_ids: [device_id],
176-
play: false
184+
play: true // Changed to true to ensure playback starts
177185
})
178186
});
179187

188+
// Then, after a short delay, play the track if specified
180189
const trackId = getUrlParameter('track');
181190
if (trackId) {
182-
try {
183-
await fetch('https://api.spotify.com/v1/me/player/play', {
184-
method: 'PUT',
185-
headers: {
186-
'Authorization': `Bearer ${token}`,
187-
'Content-Type': 'application/json'
188-
},
189-
body: JSON.stringify({
190-
uris: [`spotify:track:${trackId}`]
191-
})
192-
});
193-
} catch (error) {
194-
console.error('Error playing track:', error);
195-
}
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);
196208
}
197209
} catch (error) {
198210
console.error('Error during player initialization:', error);

0 commit comments

Comments
 (0)