Using Spotify Connect, it's possible to control the playback of the currently authenticated user.
The SpotifyWebAPI::getMyDevices()
method can be used to list out a user's devices.
// Get the Devices
$api->getMyDevices();
It is worth noting that if all devices have is_active
set to false
then using SpotifyWebApi::play()
will fail.
// With Device ID
$api->play($deviceId, [
'uris' => ['TRACK_URI'],
]);
// Without Device ID
$api->play(false, [
'uris' => ['TRACK_URI'],
]);
$api->pause();
$api->previous();
$api->next();
$api->queue('TRACK_ID');
$api->getMyQueue();
$api->getMyCurrentTrack();
$api->getMyCurrentPlaybackInfo();
$api->seek([
'position_ms' => 60000 + 37000, // Move to the 1.37 minute mark
]);
$api->repeat([
'state' => 'track',
]);
$api->shuffle([
'state' => false,
]);
$api->changeVolume([
'volume_percent' => 78,
]);
Sometimes, a API call might return a 202 Accepted
response code. When this occurs, you should retry the request after a few seconds. For example:
try {
$wasPaused = $api->pause():
if (!$wasPaused) {
$lastResponse = $api->getLastResponse();
if ($lastResponse['status'] == 202) {
// Perform some logic to retry the request after a few seconds
}
}
} catch (Exception $e) {
$reason = $e->getReason();
// Check the reason for the failure and handle the error
}
Read more about working with Spotify Connect in the Spotify API docs.