Android TTS不说话

编程入门 行业动态 更新时间:2024-10-11 05:23:51
本文介绍了Android TTS不说话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在活动"中实现android的文本到语音技术,但遇到一个奇怪的错误.我的代码中听不到任何声音.仅当我将其放在onInit方法中时,speak方法才起作用,否则它不会说话.

I am trying to implement text to speech technology of android in my Activity but I face a strange error. I can't hear any sound, from my code. The speak method works only if I place it in onInit method, else it doesn't speak.

我的代码如下:

public class GameOverActivity extends Activity implements OnInitListener { private TextToSpeech talker; .... talker = new TextToSpeech(this, this); say("Something",false); ... public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { talker.setLanguage(Locale.US); } else if (status == TextToSpeech.ERROR) { Toast.makeText(this,"Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show(); } void say(String text, boolean flush) { if(flush == true) { talker.speak(text,TextToSpeech.QUEUE_FLUSH,null); } if(flush == false) { talker.speak(text,TextToSpeech.QUEUE_ADD,null); } }

奇怪的是,如果我将say方法放在onInit上,它可以正常工作!

The strange thing is that if I place the say method in onInit, it works fine!

我仔细观察了logcat,结果如下:

I watched logcat carefully and here are the results :

TtsService.OnCreate()TT正在加载AudioTrack开始TTSService.setLanguage成功加载美国将语音速率设置为100

TtsService.OnCreate () TTs is loading AudioTrack started TTSService.setLanguage loaded en-US succusfully setting speech rate to 100

然后什么也没发生.

关于上述代码有什么问题的任何想法吗?

Any idea about what is wrong with the above code?

提前谢谢!

推荐答案

再看几个小时的代码后,我注意到问题是TTS引擎初始化需要一些时间.如果初始化尚未结束,则语音方法调用将失败.

After some more hours looking the code, I noticed that the problem is that TTS engine initialization takes some time. If initialization is not over, the speak method call will fail.

如果您在单击按钮时说出"某些内容,则可能将不需要此按钮,因为用户在按下按钮之前会花一些时间思考,并且初始化将结束.

If you "say" something on button click, you will probably won't need this, because user will take some time to think before pressing the button, and the initialization will be over.

如果您想在初始化完成后立即说"一些东西,请使用以下代码:

If you want to "say" something as soon initialization finishes, use this code :

talker = new TextToSpeech(this, new TextToSpeech.OnInitListener() { @Override public void onInit(int arg0) { if(arg0 == TextToSpeech.SUCCESS) { talker.setLanguage(Locale.US); say(gameover,true); say(line,false); say(definition_string,false); } } });

更多推荐

Android TTS不说话

本文发布于:2023-11-27 21:46:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639711.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不说话   Android   TTS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!