Created
March 28, 2018 15:52
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Métodos:
Exemplo de uso: