tags:

views:

36

answers:

1

i have a service from where i am trying to start a TextToSpeech engine ,but it seems that it's not working , so is it possible to start tts from a service?

here's what i've tried:

package com.example.TextSpeaker;

import java.util.Locale;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
 import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.widget.Toast;


 public class SpeakerService extends Service implements OnInitListener{

   public static TextToSpeech mtts;
  @Override
   public IBinder onBind(Intent arg0) {
 // TODO Auto-generated method stub
  return null;
  }

@Override
 public void onCreate(){
Log.d("SpeakerService","Service created successfully!");
mtts = new TextToSpeech(this,this);
mtts.setLanguage(Locale.ENGLISH);


}
@Override
public void onStart(Intent intent,int startid)
  {
Log.d("SpeakerService","Service started successfully!");
 Log.d("SpeakerService","Service started successfully!");
  Log.d("SpeakerService","tspker.mtts = " + TextSpeaker.mtts.toString());
 mtts = new TextToSpeech(this,this);
   mtts.setLanguage(Locale.ENGLISH);
   mtts.speak(Receiver.str, TextToSpeech.QUEUE_FLUSH,null);
}
 @Override
 public void onDestroy(){
 if(mtts!=null)
  {
     mtts.stop();
  Toast.makeText(getApplicationContext(),"The service has been destroyed!", T  oast.LENGTH_SHORT).show();
}

}

 @Override
  public void onInit(int arg0) {
   // TODO Auto-generated method stub

 }

}
A: 

new TextToSpeech(getApplicationContext(),this);

works with me... But be sure to let the service run , until speech is done..

arnold
"But be sure to let the service run , until speech is done.. "Do you mean that it will run if i do not kill the service manually through settings ?
pranay
from : http://android-developers.blogspot.com/2009/09/introduction-to-text-to-speech-in.html When not in use...The text-to-speech functionality relies on a dedicated service shared across all applications that use that feature. When you are done using TTS, be a good citizen and tell it "you won't be needing its services anymore" by calling mTts.shutdown(), in your Activity onDestroy() method for instance.
arnold