Skip to content

Instantly share code, notes, and snippets.

@damarnez
Created July 4, 2013 07:58
Show Gist options
  • Save damarnez/5925758 to your computer and use it in GitHub Desktop.
Save damarnez/5925758 to your computer and use it in GitHub Desktop.
Clase Speaker para facilita la implementación r el TextToSpeech
import java.util.HashMap;
import java.util.Locale;
import android.content.Context;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.util.Log;
public class Speaker implements TextToSpeech.OnInitListener {
private TextToSpeech tts;
Context context;
public static final int ID = 101;
public static final int CHECK_VOICE_DATA_PASS = TextToSpeech.Engine.CHECK_VOICE_DATA_PASS;
Intent checkTTSIntent;
public Speaker(Context context) {
this.context = context;
checkTTSIntent = new Intent();
checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
}
// Config speaker
public void initialize(){
Log.d("Speaker","has context"+context);
tts = new TextToSpeech(context, this);
}
public void stop(){
if (tts != null) {
tts.stop();
tts.shutdown();
}
}
//Speak functions
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.getDefault());
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
//ok is activated to speak
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
public void speakWords(String speech) {
boolean b = true;
HashMap<String, String> params = new HashMap<String, String>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId");
tts.speak(speech, TextToSpeech.QUEUE_FLUSH,params);
}
//Intents
public Intent getIntent(){
return checkTTSIntent;
}
public Intent getIntentInstall(){
Intent installTTSIntent = new Intent();
installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
return installTTSIntent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment