Skip to content

Commit 1c81d53

Browse files
committed
- playing around with the spotify API
1 parent e24c33c commit 1c81d53

File tree

1 file changed

+64
-22
lines changed

1 file changed

+64
-22
lines changed

tools/spotify_test.html

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,81 @@
33
<head>
44
<title>Spotify Track Player</title>
55
<style>
6-
body { font-family: Arial, sans-serif; max-width: 800px; margin: 2rem auto; padding: 0 1rem; }
7-
.credentials { display: flex; flex-direction: column; gap: 1rem; margin-bottom: 2rem; }
8-
input { padding: 0.5rem; width: 100%; }
9-
button { padding: 0.5rem 1rem; cursor: pointer; }
10-
.hidden { display: none; }
11-
#status { margin: 1rem 0; padding: 1rem; background: #f0f0f0; }
6+
body {
7+
font-family: Arial, sans-serif;
8+
max-width: 800px;
9+
margin: 2rem auto;
10+
padding: 0 1rem;
11+
}
12+
.credentials {
13+
display: flex;
14+
flex-direction: column;
15+
gap: 1rem;
16+
margin-bottom: 2rem;
17+
}
18+
input {
19+
padding: 0.5rem;
20+
width: 100%;
21+
}
22+
.setup-button {
23+
padding: 0.5rem 1rem;
24+
cursor: pointer;
25+
}
26+
.hidden {
27+
display: none;
28+
}
29+
#player {
30+
display: flex;
31+
justify-content: center;
32+
align-items: center;
33+
min-height: 80vh;
34+
}
35+
#playPauseButton {
36+
width: 100px;
37+
height: 100px;
38+
border-radius: 50%;
39+
border: none;
40+
background-color: #1DB954;
41+
color: white;
42+
font-size: 24px;
43+
cursor: pointer;
44+
transition: transform 0.2s;
45+
}
46+
#playPauseButton:hover {
47+
transform: scale(1.1);
48+
}
1249
</style>
1350
</head>
1451
<body>
1552
<div id="setup" class="credentials">
1653
<input type="text" id="clientId" placeholder="Enter Spotify Client ID">
17-
<button onclick="startAuth()">Initialize Player</button>
54+
<button class="setup-button" onclick="startAuth()">Initialize Player</button>
1855
</div>
1956
<div id="player" class="hidden">
20-
<div id="status">Status: Initializing...</div>
57+
<button id="playPauseButton">▐▐</button>
2158
</div>
2259

2360
<script>
2461
let playerInstance = null;
62+
let isPlaying = false;
2563
const redirectUri = 'http://gptgames.dev/tools/spotify_test.html';
26-
const statusDiv = document.getElementById('status');
64+
const playPauseButton = document.getElementById('playPauseButton');
65+
66+
function togglePlayPause() {
67+
if (isPlaying) {
68+
playerInstance.pause();
69+
} else {
70+
playerInstance.resume();
71+
}
72+
}
2773

28-
function updateStatus(message) {
29-
console.log(message);
30-
statusDiv.textContent = `Status: ${message}`;
74+
function updatePlayPauseButton(playing) {
75+
isPlaying = playing;
76+
playPauseButton.textContent = isPlaying ? '▐▐' : '▶';
3177
}
3278

3379
function getUrlParameter(name) {
3480
const urlParams = new URLSearchParams(window.location.search);
35-
console.log('getUrlParameter', name, urlParams.get(name));
3681
return urlParams.get(name);
3782
}
3883

@@ -72,34 +117,30 @@
72117
function initializePlayer(token) {
73118
document.getElementById('setup').classList.add('hidden');
74119
document.getElementById('player').classList.remove('hidden');
75-
updateStatus('Initializing player...');
76120

77121
playerInstance = new Spotify.Player({
78122
name: 'Development Player',
79123
getOAuthToken: cb => { cb(token); },
80124
volume: 0.5
81125
});
82126

83-
playerInstance.addListener('initialization_error', ({ message }) => {
84-
updateStatus(`Initialization Error: ${message}`);
127+
playerInstance.addListener('initialization_error', () => {
85128
localStorage.removeItem('spotify_access_token');
86129
});
87130

88-
playerInstance.addListener('authentication_error', ({ message }) => {
89-
updateStatus(`Authentication Error: ${message}`);
131+
playerInstance.addListener('authentication_error', () => {
90132
localStorage.removeItem('spotify_access_token');
91133
document.getElementById('setup').classList.remove('hidden');
92134
document.getElementById('player').classList.add('hidden');
93135
});
94136

95137
playerInstance.addListener('player_state_changed', state => {
96138
if (state) {
97-
updateStatus(`Now Playing: ${state.track_window.current_track.name} by ${state.track_window.current_track.artists[0].name}`);
139+
updatePlayPauseButton(!state.paused);
98140
}
99141
});
100142

101143
playerInstance.addListener('ready', ({ device_id }) => {
102-
updateStatus('Connecting to Spotify...');
103144
fetch('https://api.spotify.com/v1/me/player', {
104145
method: 'PUT',
105146
headers: {
@@ -120,6 +161,7 @@
120161
});
121162
});
122163

164+
playPauseButton.onclick = togglePlayPause;
123165
playerInstance.connect();
124166
}
125167

@@ -136,10 +178,10 @@
136178
})
137179
});
138180
if (response.status === 204) {
139-
updateStatus('Loading track...');
181+
updatePlayPauseButton(true);
140182
}
141183
} catch (error) {
142-
updateStatus('Error playing track: ' + error);
184+
console.error('Error playing track:', error);
143185
}
144186
}
145187

0 commit comments

Comments
 (0)