我正在尝试在活动"中实现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不说话
发布评论