File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 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 >
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 >
You can’t perform that action at this time.
0 commit comments