Skip to content

Instantly share code, notes, and snippets.

@AsafeSilva
Created March 28, 2018 15:52
Show Gist options
  • Select an option

  • Save AsafeSilva/20128740b97179db59489388b2d24b02 to your computer and use it in GitHub Desktop.

Select an option

Save AsafeSilva/20128740b97179db59489388b2d24b02 to your computer and use it in GitHub Desktop.
Classe com métodos que permitem a utilização do recurso de fala do google no Android
package equipe.protheus.guiderobot.utils;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.support.annotation.NonNull;
import android.util.Log;
import java.util.HashMap;
import java.util.Locale;
/**
* Created by Asafe on 17/09/2016.
*/
public class TextToVoice {
private final String TAG = "TextToVoice";
private final String UTTERANCE_ID = "SPEAK";
private TextToSpeech tts;
private Bundle paramsBundle;
private HashMap<String, String> paramsHashMap;
private boolean shutdownOnFinish;
public TextToVoice(Activity activity){
paramsBundle = new Bundle();
paramsBundle.putString(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "");
paramsHashMap = new HashMap<String, String>();
paramsHashMap.put(android.speech.tts.TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, UTTERANCE_ID);
tts = new TextToSpeech(activity, new TextToSpeech.OnInitListener(){
@Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS){
tts.setLanguage(Locale.getDefault());
}
}
});
tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
@Override
public void onStart(String s) {
// Log.d(TAG, "Start Utterance");
}
@Override
public void onDone(String s) {
// Log.d(TAG, "Done Utterance");
if(shutdownOnFinish) tts.shutdown();
}
@Override
public void onError(String s) {
// Log.d(TAG, "Error in Utterance");
}
});
}
public void Speak(final String message, final long timeToStart){
Speak(message, timeToStart, false);
}
public void shutdown(){
if(tts != null)
tts.shutdown();
}
// Na última fala na activity, chamar este método com shutdownOnFinish = true.
public void Speak(final String message, final long timeToStart, boolean shutdownOnFinish){
if(message == null) return;
this.shutdownOnFinish = shutdownOnFinish;
new CountDownTimer(timeToStart, 10){
@Override
public void onTick(long l) {}
@Override
public void onFinish() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tts.speak(message,TextToSpeech.QUEUE_FLUSH, paramsBundle, UTTERANCE_ID);
}else{
tts.speak(message, TextToSpeech.QUEUE_FLUSH, paramsHashMap);
}
}
}.start();
}
}
@AsafeSilva
Copy link
Author

AsafeSilva commented Mar 28, 2018

Métodos:

- void Speak(final String message, final long timeToStart);
- void Speak(final String message, final long timeToStart, boolean shutdownOnFinish);
- void shutdown();

Exemplo de uso:

TextToVoice speak = new TextToVoice(thisActivity.this);
...
speak.Speak("Olá, eu sou seu assistente virtual", 10);

// Ao sair da Activity
speak.shutdown();

OU

speak.Speak("Olá, eu sou seu assistente virtual", 1, true);
OBS:
Speak("msg", 10) = Speak("msg", 10, false)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment