TTS不从服务说话,而它确实是从机器人的活动 [英] TTS doesn't speak from a service whereas it does it from an activity in android

查看:122
本文介绍了TTS不从服务说话,而它确实是从机器人的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够从活动中运行TTS,但是当我尝试从服务执行同样的code,这是给我的TTS引擎被初始化的消息,但没有说什么。

I have been able to run TTS from an activity but when I try to execute the same code from a service, it's giving me message that TTS engine is initialised but not speaking anything.

有任何人任何时候遇到了同样的问题?

Has anybody encountered the same issue anytime?

 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();
}
}

任何人都可以请帮助?

Can anybody please help?

推荐答案

我有同样的问题,解决这样说: 实例化TextToSpeech对象在一个单独的类(在我来说,我还使用工厂检查Android版本至少是甜甜圈)和重新使用它(参见方法的init(上下文的背景下))。请注意,上的OnInit(INT状态)还远未准备好发布。

I had the same problem and solved it this way: Instantiate TextToSpeech Object in a separate Class (in my case I furthermore use a Factory to check if the Android Version is at least Donut) and reuse it (see method init(Context context)). Please notice that the onInit(int status) is far from being ready to release.

服务:

@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);
}}

工厂:

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();
}}

这篇关于TTS不从服务说话,而它确实是从机器人的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆