private void speakText(String textToRead, String senderStateId, int timeToWaitAfterSpeak) { if (sleepThread != null && sleepThread.isAlive()) { sleepThread.interrupt(); } HashMap<String, String> ttsParams = new HashMap<String, String>(); ttsParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, STATEMACHINE_NEXTSTATE + senderStateId + ID_AND_DURATION_SEPERATOR + timeToWaitAfterSpeak); ttsManager.getTextToSpeech().speak(textToRead, TextToSpeech.QUEUE_FLUSH, ttsParams); }
/** * Switch to the State with Id newStateId. * * @param oldStateId * @param newStateId */ public void changeState(final String oldStateId, final String newStateId) { interruptSleep(); if (ttsManager != null && ttsManager.getTextToSpeech() != null) { ttsManager.getTextToSpeech().stop(); } if (currentState != null) { currentState.directlyBeforeStateChange(oldStateId, newStateId); } // Send Broadcast that state will change Intent intent = new Intent(STATEMACHINE_WILL_CHANGE_STATE); intent.putExtra(STATEMACHINE_WILL_CHANGE_NEWSTATEID, newStateId); if (context != null) { context.sendBroadcast(intent); } if (stateModel != null && oldStateId != null && newStateId != null) { if (!stateModel.getStateMap().containsKey(newStateId)) { if (loggingEnabled) { Log.e(TAG, "Unknown state: " + newStateId); } } else { if (!isInState) { isInState = true; currentStateId = newStateId; if (stateModel.getStateMap() != null) { currentState = stateModel.getStateMap().get(newStateId); } currentState.executeInState(); isInState = false; } } } }
/** * 1. If the textToRead is the SIGNAL_TONE_LISTEND of * {@link SignalTonePlayer}, it will play the listend sound and then use the * waitBeforeNextState method of {@link AbstractActivity}. * * 2. Reads a text (add it to the {@link TextToSpeech} queue). Passes a * combination of STATEMACHINE_NEXTSTATE, the senderStateId, a separator * (ID_AND_DURATION_SEPERATOR) and timeToWaitAfterSpeak as utteranceId. * * @param textToRead * Text to read. * @param senderStateId * The state Id of the executing state. * @param timeToWaitAfterSpeak * Time to wait after speaking or playing sound. */ public void speakAndNextState(final String textToRead, final String senderStateId, final int timeToWaitAfterSpeak) { if (ttsManager != null) { if (textToRead.equals(SignalTonePlayer.SIGNAL_TONE_LISTEND)) { // SignalTonePlayer.playListendSound(context); waitBeforeNextState(timeToWaitAfterSpeak, senderStateId); } else { if (ttsManager.getTextToSpeech() == null) { Log.w(TAG, "TTS Error: ttsManager.getTextToSpeech() == null"); ttsManager.setTextToSpeechReadyForInitialization(true); new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { if (!ttsManager.ttsIsInitializing()) { break; } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } if (!ttsManager.ttsIsInitializing()) { speakText(textToRead, senderStateId, timeToWaitAfterSpeak); } } }).start(); } else { speakText(textToRead, senderStateId, timeToWaitAfterSpeak); } } } else { Log.w(TAG, "TTS Error: ttsManager == null"); } }
/** * Stop the {@link StateMachine}. Stop TTS and set current * {@link StateModel}, current {@link StateContent} and current state id to * null. */ public final void stop() { currentState = null; currentStateId = null; if (stateModel != null) { interruptSleep(); } stateModel = null; if (ttsManager.getTextToSpeech() != null) { ttsManager.getTextToSpeech().stop(); } resetRepeatCounter(); if (loggingEnabled) { Log.i(TAG, "Statemachine stopped"); } }