dwbaldeagle
New member
I succesfully initialize my TTS froma a service right after BOOT_COMPLETED, and i keep it running, the problem comes whe i try to use it from an activity. I instatiate it but it doesnt work, i mean there is no error in the code but in runtime, this notification pops out saying indicating error . here is my code. If you have any advice itll be helpful
public class Inicializacion extends Service {
public TextToSpeech tts;//creamos un objeto de tipo TextToSpeech sin instacnciarlo para utilizarlo en cualquier m?todo
private Context contexto;
private Locale localidad;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
super.onCreate();
contexto=getApplicationContext();
tts = new TextToSpeech(getApplicationContext(),ttslistener);//si tiene la librer?a instanciar TTS
}
//intanciamos la interfaz para completar la inicializaci?n
private TextToSpeech.OnInitListener ttslistener= new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
/*Toast tts_init=Toast.makeText(contexto,"TTS inicializado", Toast.LENGTH_SHORT);
tts_init.show();*/
localidad=tts.getLanguage();
if(localidad==Locale.US){
tts.setLanguage(Locale.US);
Toast tts_US=Toast.makeText(contexto,"TTS ENGLISH", Toast.LENGTH_SHORT);
tts_US.show();
}
if(localidad==Locale.ENGLISH){
tts.setLanguage(Locale.ENGLISH);
Toast tts_US=Toast.makeText(contexto,"TTS ENGLISH", Toast.LENGTH_SHORT);
tts_US.show();
}
if(localidad==Locale.FRANCE){
tts.setLanguage(Locale.FRANCE);
Toast tts_FR=Toast.makeText(contexto,"TTS FRANCES", Toast.LENGTH_SHORT);
tts_FR.show();
}
if(localidad==Locale.FRENCH){
tts.setLanguage(Locale.FRENCH);
Toast tts_FR=Toast.makeText(contexto,"TTS FRANCES", Toast.LENGTH_SHORT);
tts_FR.show();
}
if(localidad==Locale.GERMAN){
tts.setLanguage(Locale.GERMAN);
Toast tts_GE=Toast.makeText(contexto,"TTS GERMAN", Toast.LENGTH_SHORT);
tts_GE.show();
}
else{
tts.setLanguage(new Locale ("spa","ES"));
Toast tts_ES=Toast.makeText(contexto,"TTS EPA?OL", Toast.LENGTH_SHORT);
tts_ES.show();
}
Intent ttsinit=new Intent();
ttsinit.setAction("com.tes.BW.ttsinit");
sendBroadcast(ttsinit);
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
}
thats the service code. And then i use this two lines in the onCreate of the activity
final Inicializacion herramientas=new Inicializacion();
herramientas.tts.speak("boton aplastado", TextToSpeech.QUEUE_ADD, null);
public class Inicializacion extends Service {
public TextToSpeech tts;//creamos un objeto de tipo TextToSpeech sin instacnciarlo para utilizarlo en cualquier m?todo
private Context contexto;
private Locale localidad;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
super.onCreate();
contexto=getApplicationContext();
tts = new TextToSpeech(getApplicationContext(),ttslistener);//si tiene la librer?a instanciar TTS
}
//intanciamos la interfaz para completar la inicializaci?n
private TextToSpeech.OnInitListener ttslistener= new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
/*Toast tts_init=Toast.makeText(contexto,"TTS inicializado", Toast.LENGTH_SHORT);
tts_init.show();*/
localidad=tts.getLanguage();
if(localidad==Locale.US){
tts.setLanguage(Locale.US);
Toast tts_US=Toast.makeText(contexto,"TTS ENGLISH", Toast.LENGTH_SHORT);
tts_US.show();
}
if(localidad==Locale.ENGLISH){
tts.setLanguage(Locale.ENGLISH);
Toast tts_US=Toast.makeText(contexto,"TTS ENGLISH", Toast.LENGTH_SHORT);
tts_US.show();
}
if(localidad==Locale.FRANCE){
tts.setLanguage(Locale.FRANCE);
Toast tts_FR=Toast.makeText(contexto,"TTS FRANCES", Toast.LENGTH_SHORT);
tts_FR.show();
}
if(localidad==Locale.FRENCH){
tts.setLanguage(Locale.FRENCH);
Toast tts_FR=Toast.makeText(contexto,"TTS FRANCES", Toast.LENGTH_SHORT);
tts_FR.show();
}
if(localidad==Locale.GERMAN){
tts.setLanguage(Locale.GERMAN);
Toast tts_GE=Toast.makeText(contexto,"TTS GERMAN", Toast.LENGTH_SHORT);
tts_GE.show();
}
else{
tts.setLanguage(new Locale ("spa","ES"));
Toast tts_ES=Toast.makeText(contexto,"TTS EPA?OL", Toast.LENGTH_SHORT);
tts_ES.show();
}
Intent ttsinit=new Intent();
ttsinit.setAction("com.tes.BW.ttsinit");
sendBroadcast(ttsinit);
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
}
thats the service code. And then i use this two lines in the onCreate of the activity
final Inicializacion herramientas=new Inicializacion();
herramientas.tts.speak("boton aplastado", TextToSpeech.QUEUE_ADD, null);