|
3 | 3 | <head> |
4 | 4 | <title>Spotify Track Player</title> |
5 | 5 | <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 | + } |
12 | 49 | </style> |
13 | 50 | </head> |
14 | 51 | <body> |
15 | 52 | <div id="setup" class="credentials"> |
16 | 53 | <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> |
18 | 55 | </div> |
19 | 56 | <div id="player" class="hidden"> |
20 | | - <div id="status">Status: Initializing...</div> |
| 57 | + <button id="playPauseButton">▐▐</button> |
21 | 58 | </div> |
22 | 59 |
|
23 | 60 | <script> |
24 | 61 | let playerInstance = null; |
| 62 | + let isPlaying = false; |
25 | 63 | 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 | + } |
27 | 73 |
|
28 | | - function updateStatus(message) { |
29 | | - console.log(message); |
30 | | - statusDiv.textContent = `Status: ${message}`; |
| 74 | + function updatePlayPauseButton(playing) { |
| 75 | + isPlaying = playing; |
| 76 | + playPauseButton.textContent = isPlaying ? '▐▐' : '▶'; |
31 | 77 | } |
32 | 78 |
|
33 | 79 | function getUrlParameter(name) { |
34 | 80 | const urlParams = new URLSearchParams(window.location.search); |
35 | | - console.log('getUrlParameter', name, urlParams.get(name)); |
36 | 81 | return urlParams.get(name); |
37 | 82 | } |
38 | 83 |
|
|
72 | 117 | function initializePlayer(token) { |
73 | 118 | document.getElementById('setup').classList.add('hidden'); |
74 | 119 | document.getElementById('player').classList.remove('hidden'); |
75 | | - updateStatus('Initializing player...'); |
76 | 120 |
|
77 | 121 | playerInstance = new Spotify.Player({ |
78 | 122 | name: 'Development Player', |
79 | 123 | getOAuthToken: cb => { cb(token); }, |
80 | 124 | volume: 0.5 |
81 | 125 | }); |
82 | 126 |
|
83 | | - playerInstance.addListener('initialization_error', ({ message }) => { |
84 | | - updateStatus(`Initialization Error: ${message}`); |
| 127 | + playerInstance.addListener('initialization_error', () => { |
85 | 128 | localStorage.removeItem('spotify_access_token'); |
86 | 129 | }); |
87 | 130 |
|
88 | | - playerInstance.addListener('authentication_error', ({ message }) => { |
89 | | - updateStatus(`Authentication Error: ${message}`); |
| 131 | + playerInstance.addListener('authentication_error', () => { |
90 | 132 | localStorage.removeItem('spotify_access_token'); |
91 | 133 | document.getElementById('setup').classList.remove('hidden'); |
92 | 134 | document.getElementById('player').classList.add('hidden'); |
93 | 135 | }); |
94 | 136 |
|
95 | 137 | playerInstance.addListener('player_state_changed', state => { |
96 | 138 | 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); |
98 | 140 | } |
99 | 141 | }); |
100 | 142 |
|
101 | 143 | playerInstance.addListener('ready', ({ device_id }) => { |
102 | | - updateStatus('Connecting to Spotify...'); |
103 | 144 | fetch('https://api.spotify.com/v1/me/player', { |
104 | 145 | method: 'PUT', |
105 | 146 | headers: { |
|
120 | 161 | }); |
121 | 162 | }); |
122 | 163 |
|
| 164 | + playPauseButton.onclick = togglePlayPause; |
123 | 165 | playerInstance.connect(); |
124 | 166 | } |
125 | 167 |
|
|
136 | 178 | }) |
137 | 179 | }); |
138 | 180 | if (response.status === 204) { |
139 | | - updateStatus('Loading track...'); |
| 181 | + updatePlayPauseButton(true); |
140 | 182 | } |
141 | 183 | } catch (error) { |
142 | | - updateStatus('Error playing track: ' + error); |
| 184 | + console.error('Error playing track:', error); |
143 | 185 | } |
144 | 186 | } |
145 | 187 |
|
|
0 commit comments