public class TTSActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener, ... { private TextToSpeech mTts; ........... private void speak(String text) { if(text != null) { HashMap<String, String> myHashAlarm = new HashMap<String, String>(); myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM)); myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "SOME MESSAGE"); mTts.speak(text, TextToSpeech.QUEUE_FLUSH, myHashAlarm); } } // Fired after TTS initialization public void onInit(int status) { if(status == TextToSpeech.SUCCESS) { mTts.setOnUtteranceCompletedListener(this); } } // It's callback public void onUtteranceCompleted(String utteranceId) { Log.i(TAG, utteranceId); //utteranceId == "SOME MESSAGE" } ........... }
@Override public void onInit(int status) { if (status != TextToSpeech.ERROR) { mTextToSpeech.setLanguage(Locale.UK); mTextToSpeech.setSpeechRate(0.70f); } mTextToSpeech.setOnUtteranceCompletedListener( new TextToSpeech.OnUtteranceCompletedListener() { @Override public void onUtteranceCompleted(String utteranceId) { ((AppCompatActivity) context).runOnUiThread(new Runnable() { @Override public void run() { if (mIsSpeaking) { callbacks.highLightTTS(); } } }); } }); } });
@SuppressLint("NewApi") @SuppressWarnings("deprecation") public void setupOnCompleteListener() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() { @Override public void onUtteranceCompleted(String utteranceId) { Log.d(TAG, "Completed: " + utteranceId); onComplete(utteranceId); } }); } else { tts.setOnUtteranceProgressListener(new UtteranceProgressListener() { @Override public void onStart(String utteranceId) { Log.d(TAG, "Start v15: " + utteranceId); } @Override public void onError(String utteranceId) { Log.e(TAG, "Error v15: " + utteranceId); } @Override public void onDone(String utteranceId) { Log.d(TAG, "Completed v15: " + utteranceId); onComplete(utteranceId); } }); } }
@Override public void run() { ttsToShutdown.setOnUtteranceCompletedListener(null); ttsToShutdown.stop(); ttsToShutdown.shutdown(); } }.start();
tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() { @Override public void onUtteranceCompleted(String utteranceId) {
/** * Called by the system every time a client explicitly starts the service by calling startService(Intent), * providing the arguments it supplied and a unique integer token representing the start request. * Do not call this method directly. Starts the tts engine and grabs the string to speak. * * @param intent The Intent supplied to startService(Intent), as given. * @param flags Additional data about this start request. Currently either 0, START_FLAG_REDELIVERY, or * START_FLAG_RETRY. * @param startId A unique integer representing this specific request to start. Use with stopSelfResult(int). * @return START_NOT_STICKY since this service does a little work and then stops itself */ @Override public int onStartCommand(Intent intent, int flags, int startId) { mTts = new TextToSpeech(this, this); mTts.setOnUtteranceCompletedListener(this); mTextToSpeak = getString(intent.getIntExtra(TEXT_TO_SPEAK, 0)); assert getApplicationContext() != null; mAudioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE); return START_NOT_STICKY; }
int result = tts.setOnUtteranceCompletedListener(whenTextDone); if (result == TextToSpeech.ERROR)
textToSpeech.setOnUtteranceCompletedListener(this);
speaker.setOnUtteranceCompletedListener(this); btnSpeak = (Button) findViewById(R.id.btnSpeak); btnSpeak.setOnClickListener(new View.OnClickListener() {
/** * When the fragment is created, set up the TTS engine, AudioManager, and MediaPlayer for life total vocalization * * @param savedInstanceState If the fragment is being re-created from a previous saved state, this is the state. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mTtsInit = false; mTts = new TextToSpeech(getActivity(), this); mTts.setOnUtteranceCompletedListener(this); mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE); m9000Player = MediaPlayer.create(getActivity(), R.raw.over_9000); if (m9000Player != null) { m9000Player.setOnCompletionListener(mp -> onUtteranceCompleted(LIFE_ANNOUNCE)); } }
this.tts.setOnUtteranceCompletedListener(new TextToSpeech.OnUtteranceCompletedListener() { @Override public void onUtteranceCompleted(String utteranceId) {