duncan3dc\Sonos\Controller::restoreState PHP Method

restoreState() public method

Restore the Controller to a previously exported state.
public restoreState ( duncan3dc\Sonos\ControllerState $state ) : static
$state duncan3dc\Sonos\ControllerState The state to be restored
return static
    public function restoreState(ControllerState $state)
    {
        $queue = $this->getQueue();
        $queue->clear();
        if (count($state->tracks) > 0) {
            $queue->addTracks($state->tracks);
        }
        if (count($state->tracks) > 0) {
            $this->selectTrack($state->track);
            list($hours, $minutes, $seconds) = explode(":", $state->position);
            $time = ($hours * 60 + $minutes) * 60 + $seconds;
            $this->seek($time);
        }
        $this->setShuffle($state->shuffle);
        $this->setRepeat($state->repeat);
        $this->setCrossfade($state->crossfade);
        if ($state->stream) {
            $this->useStream($state->stream);
        }
        $speakers = [];
        foreach ($this->getSpeakers() as $speaker) {
            $speakers[$speaker->getUuid()] = $speaker;
        }
        foreach ($state->speakers as $uuid => $volume) {
            if (array_key_exists($uuid, $speakers)) {
                $speakers[$uuid]->setVolume($volume);
            }
        }
        # If the exported state was playing then start it playing now
        if ($state->state === self::STATE_PLAYING) {
            $this->play();
            # If the exported state was stopped and we are playing then stop it now
        } elseif ($this->getState() === self::STATE_PLAYING) {
            $this->pause();
        }
        return $this;
    }