简体   繁体   English

文本到语音android无法正常工作

[英]Text to Speech android not working

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    init();
}

public void init() {
    tts = new TextToSpeech(Game.this, new OnInitListener() {
        public void onInit(int status) {
            if (status != TextToSpeech.ERROR) {
                 tts.setLanguage(Locale.US);
                 speakout("Hello Gies");
            }
        }
    });
}

public void speakout(String text) {
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    tv2.setText("" + text);

}

@Override
protected void onPause() {
    if (tts != null) {
        tts.stop();
        tts.shutdown();
    }
    super.onPause();
}

The code runs fine. 代码运行正常。 I am trying to covert the text to speech but the I am unable to find the desired output. 我试图将文本转换为语音,但我无法找到所需的输出。 Please help me out with a fix. 请帮我解决问题。 Thanks in advance. 提前致谢。

You should not use tts reference before onInit is called with success, and from your code its clear you are calling speakout just after creating TextToSpeech class. 在成功调用onInit之前不应该使用tts引用,并且从代码中可以清楚地知道在创建TextToSpeech类之后调用speakout。 Move speakout("Hello Gies"); 移动speakout("Hello Gies"); to inside your onInit . 到你的onInit里面。 Also if you shutdown your tts in onPause then you better recreate it in onResume - this means you can actually move your init(); 此外,如果你在onPause中关闭你的tts然后你最好在onResume中重新创建它 - 这意味着你可以实际移动你的init(); to onResume . onResume

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM