duncan3dc\Sonos\Controller::interrupt PHP Method

interrupt() public method

The current state of the controller is stored, the passed track is played, and then when it has finished the previous state of the controller is restored. This is useful for making announcements over the Sonos network.
public interrupt ( duncan3dc\Sonos\Tracks\UriInterface $track, integer $volume = null ) : static
$track duncan3dc\Sonos\Tracks\UriInterface The track to play
$volume integer The volume to play the track at
return static
    public function interrupt(UriInterface $track, $volume = null)
    {
        /**
         * Ensure the track has been generated.
         * If it's a TextToSpeech then the api call is done lazily when the uri is required.
         * So it's better to do this here, rather than after the controller has been paused.
         */
        $track->getUri();
        $state = $this->exportState();
        # Replace the current queue with the passed track
        $this->useQueue()->getQueue()->clear()->addTrack($track);
        # Ensure repeat is not on, or else this track would just play indefinitely
        $this->setRepeat(false);
        # If a volume was passed then use it
        if ($volume !== null) {
            $this->setVolume($volume);
        }
        # Play the track
        $this->play();
        # Sleep first so that the track has a chance to at least start
        sleep(1);
        # Wait for the track to finish
        while ($this->getState() === self::STATE_PLAYING) {
            usleep(500000);
        }
        # Restore the previous state of this controller
        $this->restoreState($state);
        return $this;
    }