Sonos A PHP library for interacting with Sonos speakers

Getting Started

Usage

Controllers

Services

Fun Stuff

Playlists

View the API Documentation for this class

You can get existing playlists from the Network class or create a new one like so:

$playlist = $sonos->createPlaylist("Daniel Tompkins");
echo "{$playlist->getName()}\n";

# Changed my mind, delete the playlist from the network
$playlist->delete();

The Playlist class implements the Countable interface which means you can get the number of tracks by simply counting it:

$numberOfTracks = count($playlist);

# Or call the actual count method
$numberOfTracks = $playlist->count();

You can empty a playlist using the clear method:

$playlist->clear();

You can list all the tracks from a playlist:

$playlist = $sonos->getPlaylistByName("progmetal");

foreach ($playlist->getTracks() as $track) {
    echo "* " . $track->getArtist() . " - " . $track->getTitle() . "\n";
}

The getTracks() method returns an array of Tracks.

Remove tracks from a playlist:

$progmetal = $sonos->getPlaylistByName("progmetal");

$remove = [];
foreach ($progmetal->getTracks() as $position => $track) {
    if ($track->getArtist() === "protest the hero") {
        $remove[] = $position;
    }
}
if (count($remove) > 0) {
    $progmetal->removeTracks($remove);
}

This is done using a single call to removeTracks() because all the positions will be recalculated once a track has been removed, so the other positions would now be invalid. It's also more efficient as we only send one request to the Sonos network instead of many.