drewbietron
drewbietron

Reputation: 229

Spotify Web Playback SDK - Play Full Song

I am trying to use the Spotify Web Playback SDK to play full songs in a browser. Following the docs I am still confused of how to actually play a song using this API. I can control playback/volume etc of a song thats already being played using my account, but there are no endpoints in the API to give it a track payload and have it play back that song.

Any help is appreciated.

Upvotes: 2

Views: 7058

Answers (1)

arirawr
arirawr

Reputation: 1275

You need to use the Spotify Connect Web API (docs here: https://beta.developer.spotify.com/documentation/web-api/reference/player/)

Using the device_id you get from the Web Playback SDK, simply make a call to /me/player/play. Here's how that would look with AJAX:

  $.ajax({
   url: "https://api.spotify.com/v1/me/player/play?device_id=" + device_id,
   type: "PUT",
   data: '{"uris": ["spotify:track:5ya2gsaIhTkAuWYEMB0nw5"]}',
   beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'Bearer ' + _token );},
   success: function(data) { 
     console.log(data)
   }
  });

Check out this simple Glitch example to get started: https://glitch.com/edit/#!/spotify-web-playback

Upvotes: 4

Related Questions