【问题标题】:Text to speech Manager doesn't work文字转语音管理器不起作用
【发布时间】:2012-08-25 09:35:51
【问题描述】:

我创建了一些 TTS 管理器,因为我想在其他课程中使用 someTTsObject.speak("some string")。 这是我的经理类:

public class TtsManager
{
private TextToSpeech myTTS;
private Context context;

public TtsManager(Context baseContext)
{
    this.context = baseContext;
    initOrInstallTts();
}

public void initOrInstallTts()
{
    myTTS = new TextToSpeech(context, new OnInitListener() 
    {               
        public void onInit(int status) 
        {
            if (status == TextToSpeech.SUCCESS)
            {
                myTTS.setLanguage(Locale.US);
            }
            else
                installTts();
        }
    });
}

private void installTts()
{
    Intent installIntent = new Intent();
    installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
    context.startActivity(installIntent);
}

public void speak(String text)
{       
        myTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}

}

这是我的主要课程,我想在其中使用它:

public class main extends Activity {
TtsManager tts;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tts = new TtsManager(this);
        tts.speak("Welcome in my app");

    }
}

当我运行编译时,我在 LogCat 上看到:

08-30 17:25:52.531: I/TTS received:(2782): Welcome in my app

但我没有听到任何文字。我在虚拟机和手机上测试过。

为什么这不起作用? 干杯!

【问题讨论】:

    标签: android voice speech text-to-speech


    【解决方案1】:

    好的,问题是您没有等到系统的回调告诉您 TTS 已初始化。在使用 SUCCESS 值调用 onInit 之前,您无法调用 speak。

    【讨论】:

      【解决方案2】:

      问题是您在没有初始化 tts 引擎的情况下调用了 speak 函数.... 添加 tts.initOrInstallTts();tts = new TtsManager(this);

      喜欢:

      public class main extends Activity {
          TtsManager tts;
          @Override
          protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
          tts = new TtsManager(this);
          tts.initOrInstallTts();
          tts.speak("Welcome in my app");
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-14
        • 2015-03-11
        • 1970-01-01
        相关资源
        最近更新 更多