TTS does not speak from the service, whereas it does it from activity in android

I managed to start TTS from the action, but when I try to execute the same code from the service, it gives me a message that the TTS mechanism is initialized, but does not say anything.

Has anyone encountered the same problem at any time?

 public void onCreate() {

    super.onCreate();
    tts = new TextToSpeech(this, this //TextToSpeech.OnInitListener);

    timer.scheduleAtFixedRate( new TimerTask()                       
    {                                   // In timer

          public void run() {
          //On some condition
          tts.speak("thank you", TextToSpeech.QUEUE_ADD, null);
     }, 0, 60000);
    }

  @Override
  public void onInit(int status) {  
  if (status == TextToSpeech.SUCCESS) {
  Toast.makeText(BackgroundProcessforTimecheck.this, 
  "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
 }
 else if (status == TextToSpeech.ERROR) {
  Toast.makeText(BackgroundProcessforTimecheck.this, 
  "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
}
}
0
source share
2 answers

, : TextToSpeech ( Factory, , Android ) (. init ( )). , onInit ( int) .

:

@Override
public void onStart(Intent intent, int startId) {
Context context = getApplicationContext();
TtsProviderFactory ttsProviderImpl = TtsProviderFactory.getInstance();
if (ttsProviderImpl != null) {
    ttsProviderImpl.init(context);
    ttsProviderImpl.say("hope that helps);
}}

Factory:

public abstract class TtsProviderFactory {

public abstract void say(String sayThis);

public abstract void init(Context context);

public abstract void shutdown();


private static TtsProviderFactory sInstance;

public static TtsProviderFactory getInstance() {
    if (sInstance == null) {
        int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
        if (sdkVersion < Build.VERSION_CODES.DONUT) {
            return null;
        }

        try {
            String className = "TtsProviderImpl";
            Class<? extends TtsProviderFactory> clazz =
                    Class.forName(TtsProviderFactory.class.getPackage().getName() + "." + className)
                            .asSubclass(TtsProviderFactory.class);
            sInstance = clazz.newInstance();
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
    return sInstance;
}}

:

public class TtsProviderImpl extends TtsProviderFactory implements TextToSpeech.OnInitListener {

private TextToSpeech tts;

public void init(Context context) {
    if (tts == null) {
        tts = new TextToSpeech(context, this);
    }
}

@Override
public void say(String sayThis) {
    tts.speak(sayThis, TextToSpeech.QUEUE_FLUSH, null);
}

@Override
public void onInit(int status) {
    Locale loc = new Locale("de", "", "");
    if (tts.isLanguageAvailable(loc) >= TextToSpeech.LANG_AVAILABLE) {
        tts.setLanguage(loc);
    }
}

public void shutdown() {
    tts.shutdown();
}}
+6

TTS . . , TTS .

0

All Articles