我一直在网上冲浪(阅读google / android开发者文档,阅读stackoverflow的类似问题,阅读书籍),并密切关注这些示例。文本到语音“说话”很有效。但我无法让OnUtteranceCompleted被调用。
一定是这么简单,我没有看到答案。请帮忙!这是几次互动后的代码。
或者有人可以提供一个完整的源代码(不是片段),实际上可以查看它是否可以在我的模拟器/实际设备上运行?
public class testActivity extends Activity implements OnInitListener, OnUtteranceCompletedListener {
...
protected void checkTtS() {
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, TTS_DATA_CHECK_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
....
if (requestCode == TTS_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
mTts = new TextToSpeech(this, this);
....
// Implements TextToSpeech.OnInitListener.
public void onInit(int status) {
// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
if (status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.FRANCE);
result = mTts.setOnUtteranceCompletedListener(this);
HashMap<String, String> params = new HashMap<String, String>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId");
mTts.speak("Text to Speak",TextToSpeech.QUEUE_FLUSH, params);
....
public void onUtteranceCompleted(String uttId) {
Toast.makeText(getBaseContext(), "onutterancecompleted", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:0)
在setOnUtteranceCompletedListener调用之后检查了int结果的值。
if(result == TextToSpeech.ERROR)
然后没有设置话语听众
答案 1 :(得分:-1)
您的代码可能应如下所示:
public void onUtteranceCompleted(String uttId) {
if (uttId.equals("stringId")) {
Toast.makeText(getBaseContext(), "onutterancecompleted", Toast.LENGTH_SHORT).show();
}
}
您还应该查看this文章,其中有onUtteranceCompleted
如何运作的明确说明。希望这会有所帮助。