public static boolean squeezeboxSpeak(
@ParamDoc(name = "playerId", text = "The Squeezebox to send the message to") String playerId,
@ParamDoc(name = "message", text = "The message to say") String message,
@ParamDoc(name = "volume", text = "The volume to set the device when speaking this message (between 1-100)") int volume,
@ParamDoc(name = "resumePlayback", text = "Continue playback after speech") Boolean resumePlayback ){
SqueezePlayer player = getPlayer(playerId);
if (player == null) return false;
logger.trace("***START SPEECH**** Player: '{}'", playerId);
// get the current player state
int playerVolume = player.getUnmuteVolume();
boolean playerPowered = player.isPowered();
boolean playerMuted = player.isMuted();
Mode playerMode = player.getMode();
int currNumTracks = player.getNumberPlaylistTracks();
int currPlayingTime = player.getCurrentPlayingTime();
int currPlaylistIndex = player.getCurrentPlaylistIndex();
int currPlaylistShuffle = player.getCurrentPlaylistShuffle();
int currPlaylistRepeat = player.getCurrentPlaylistRepeat();
int newNumTracks = 0;
logger.trace("Current Playing Mode '{}'", playerMode.toString());
logger.trace("Current Volume '{}'", playerVolume);
logger.trace("Current Num Playlist Tracks '{}'", currNumTracks);
logger.trace("Current Playing Playlist Index '{}'", currPlaylistIndex);
logger.trace("Current Playing Time '{}'", currPlayingTime);
logger.trace("Current Shuffle Mode '{}'", currPlaylistShuffle);
logger.trace("Current Repeat Mode '{}'", currPlaylistRepeat);
// If Playing Pause player before adjusting volume!
if (playerMode == Mode.play)
squeezeServer.pause(playerId);
// set the player ready to play this announcement
if (playerMuted) {
logger.trace("Setting player state: unmuted");
squeezeServer.unMute(playerId);
}
if (volume != -1) {
logger.trace("Setting player state: volume {}", volume);
squeezeServer.setVolume(playerId, volume);
}
if (currPlaylistRepeat != 0)
squeezeServer.setRepeatMode(playerId, 0);
if (currPlaylistShuffle != 0) {
squeezeServer.setShuffleMode(playerId, 0);
currPlaylistIndex = 0;
logger.trace("Shuffle Changed! Set Current Playing Index to 0");
}
// can only 'say' 100 chars at a time
List<String> sentences = getSentences(message, MAX_SENTENCE_LENGTH);
// send each sentence in turn
for (String sentence : sentences) {
logger.trace("Sending sentence to " + playerId + " (" + sentence + ")");
String encodedSentence;
try {
encodedSentence = URLEncoder.encode(sentence, "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.warn("Failed to encode sentence '" + sentence + "'. Skipping sentence.", e);
continue;
}
encodedSentence = encodedSentence.replace("+", "%20");
logger.trace("Encoded sentence " + encodedSentence);
// build the URL to send to the Squeezebox to play
String url = String.format(GOOGLE_TRANSLATE_URL,squeezeServer.language()) + encodedSentence;
// create an instance of our special listener so we can detect when the sentence is complete
SqueezeboxSentenceListener listener = new SqueezeboxSentenceListener(playerId);
squeezeServer.addPlayerEventListener(listener);
// send the URL (this will power up the player and un-mute if necessary)
logger.trace("Adding URL to current playlist '{}' to play", url);
squeezeServer.addPlaylistItem(playerId, url);
logger.trace("Sleeping for 1s for updated playlist to refresh", url);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
continue;
}
newNumTracks = player.getNumberPlaylistTracks();
logger.trace("New Playlist Track Number: '{}'", newNumTracks);
squeezeServer.playPlaylistItem(playerId, newNumTracks-1);
squeezeServer.play(playerId);